@@ -436,9 +436,11 @@ class C[T]:
436
436
class Inner[U](make_base(T for _ in (1,)), make_base(T)):
437
437
pass
438
438
"""
439
- with self .assertRaisesRegex (SyntaxError ,
440
- "Cannot use comprehension in annotation scope within class scope" ):
441
- run_code (code )
439
+ ns = run_code (code )
440
+ inner = ns ["C" ].Inner
441
+ base1 , base2 , _ = inner .__bases__
442
+ self .assertEqual (list (base1 .__arg__ ), [ns ["C" ].__type_params__ [0 ]])
443
+ self .assertEqual (base2 .__arg__ , "class" )
442
444
443
445
def test_listcomp_in_nested_class (self ):
444
446
code = """
@@ -464,9 +466,11 @@ class C[T]:
464
466
class Inner[U](make_base([T for _ in (1,)]), make_base(T)):
465
467
pass
466
468
"""
467
- with self .assertRaisesRegex (SyntaxError ,
468
- "Cannot use comprehension in annotation scope within class scope" ):
469
- run_code (code )
469
+ ns = run_code (code )
470
+ inner = ns ["C" ].Inner
471
+ base1 , base2 , _ = inner .__bases__
472
+ self .assertEqual (base1 .__arg__ , [ns ["C" ].__type_params__ [0 ]])
473
+ self .assertEqual (base2 .__arg__ , "class" )
470
474
471
475
def test_gen_exp_in_generic_method (self ):
472
476
code = """
@@ -475,27 +479,33 @@ class C[T]:
475
479
def meth[U](x: (T for _ in (1,)), y: T):
476
480
pass
477
481
"""
478
- with self .assertRaisesRegex (SyntaxError ,
479
- "Cannot use comprehension in annotation scope within class scope" ):
480
- run_code (code )
482
+ ns = run_code (code )
483
+ meth = ns ["C" ].meth
484
+ self .assertEqual (list (meth .__annotations__ ["x" ]), [ns ["C" ].__type_params__ [0 ]])
485
+ self .assertEqual (meth .__annotations__ ["y" ], "class" )
481
486
482
487
def test_nested_scope_in_generic_alias (self ):
483
488
code = """
484
- class C[T]:
489
+ T = "global"
490
+ class C:
485
491
T = "class"
486
492
{}
487
493
"""
488
- error_cases = [
489
- "type Alias3 [T] = (T for _ in (1,))" ,
490
- "type Alias4 = (T for _ in (1,))" ,
491
- "type Alias5 [T] = [T for _ in (1,)]" ,
492
- "type Alias6 = [T for _ in (1,)]" ,
494
+ cases = [
495
+ "type Alias [T] = (T for _ in (1,))" ,
496
+ "type Alias = (T for _ in (1,))" ,
497
+ "type Alias [T] = [T for _ in (1,)]" ,
498
+ "type Alias = [T for _ in (1,)]" ,
493
499
]
494
- for case in error_cases :
500
+ for case in cases :
495
501
with self .subTest (case = case ):
496
- with self .assertRaisesRegex (SyntaxError ,
497
- r"Cannot use [a-z]+ in annotation scope within class scope" ):
498
- run_code (code .format (case ))
502
+ ns = run_code (code .format (case ))
503
+ alias = ns ["C" ].Alias
504
+ value = list (alias .__value__ )[0 ]
505
+ if alias .__type_params__ :
506
+ self .assertIs (value , alias .__type_params__ [0 ])
507
+ else :
508
+ self .assertEqual (value , "global" )
499
509
500
510
def test_lambda_in_alias_in_class (self ):
501
511
code = """
0 commit comments