@@ -412,6 +412,99 @@ def test_comprehension_02(self):
412
412
func , = T .__bound__
413
413
self .assertEqual (func (), 1 )
414
414
415
+ def test_gen_exp_in_nested_class (self ):
416
+ code = """
417
+ from test.test_type_params import make_base
418
+
419
+ class C[T]:
420
+ T = "class"
421
+ class Inner(make_base(T for _ in (1,)), make_base(T)):
422
+ pass
423
+ """
424
+ C = run_code (code )["C" ]
425
+ T , = C .__type_params__
426
+ base1 , base2 = C .Inner .__bases__
427
+ self .assertEqual (list (base1 .__arg__ ), [T ])
428
+ self .assertEqual (base2 .__arg__ , "class" )
429
+
430
+ def test_gen_exp_in_nested_generic_class (self ):
431
+ code = """
432
+ from test.test_type_params import make_base
433
+
434
+ class C[T]:
435
+ T = "class"
436
+ class Inner[U](make_base(T for _ in (1,)), make_base(T)):
437
+ pass
438
+ """
439
+ with self .assertRaisesRegex (SyntaxError ,
440
+ "Cannot use comprehension in annotation scope within class scope" ):
441
+ run_code (code )
442
+
443
+ def test_listcomp_in_nested_class (self ):
444
+ code = """
445
+ from test.test_type_params import make_base
446
+
447
+ class C[T]:
448
+ T = "class"
449
+ class Inner(make_base([T for _ in (1,)]), make_base(T)):
450
+ pass
451
+ """
452
+ C = run_code (code )["C" ]
453
+ T , = C .__type_params__
454
+ base1 , base2 = C .Inner .__bases__
455
+ self .assertEqual (base1 .__arg__ , [T ])
456
+ self .assertEqual (base2 .__arg__ , "class" )
457
+
458
+ def test_listcomp_in_nested_generic_class (self ):
459
+ code = """
460
+ from test.test_type_params import make_base
461
+
462
+ class C[T]:
463
+ T = "class"
464
+ class Inner[U](make_base([T for _ in (1,)]), make_base(T)):
465
+ pass
466
+ """
467
+ with self .assertRaisesRegex (SyntaxError ,
468
+ "Cannot use comprehension in annotation scope within class scope" ):
469
+ run_code (code )
470
+
471
+ def test_gen_exp_in_generic_method (self ):
472
+ code = """
473
+ class C[T]:
474
+ T = "class"
475
+ def meth[U](x: (T for _ in (1,)), y: T):
476
+ pass
477
+ """
478
+ with self .assertRaisesRegex (SyntaxError ,
479
+ "Cannot use comprehension in annotation scope within class scope" ):
480
+ run_code (code )
481
+
482
+ def test_nested_scope_in_generic_alias (self ):
483
+ code = """
484
+ class C[T]:
485
+ T = "class"
486
+ {}
487
+ """
488
+ error_cases = [
489
+ "type Alias1[T] = lambda: T" ,
490
+ "type Alias2 = lambda: T" ,
491
+ "type Alias3[T] = (T for _ in (1,))" ,
492
+ "type Alias4 = (T for _ in (1,))" ,
493
+ "type Alias5[T] = [T for _ in (1,)]" ,
494
+ "type Alias6 = [T for _ in (1,)]" ,
495
+ ]
496
+ for case in error_cases :
497
+ with self .subTest (case = case ):
498
+ with self .assertRaisesRegex (SyntaxError ,
499
+ r"Cannot use [a-z]+ in annotation scope within class scope" ):
500
+ run_code (code .format (case ))
501
+
502
+
503
+ def make_base (arg ):
504
+ class Base :
505
+ __arg__ = arg
506
+ return Base
507
+
415
508
416
509
def global_generic_func [T ]():
417
510
pass
0 commit comments