@@ -387,10 +387,21 @@ class Nested: ...
387
387
self .assertEqual (Outer .__annotations__ , {"x" : Outer .Nested })
388
388
389
389
def test_no_exotic_expressions (self ):
390
- check_syntax_error (self , "def func(x: (yield)): ..." , "yield expression cannot be used within an annotation" )
391
- check_syntax_error (self , "def func(x: (yield from x)): ..." , "yield expression cannot be used within an annotation" )
392
- check_syntax_error (self , "def func(x: (y := 3)): ..." , "named expression cannot be used within an annotation" )
393
- check_syntax_error (self , "def func(x: (await 42)): ..." , "await expression cannot be used within an annotation" )
390
+ preludes = [
391
+ "" ,
392
+ "class X:\n " ,
393
+ "def f():\n " ,
394
+ "async def f():\n " ,
395
+ ]
396
+ for prelude in preludes :
397
+ with self .subTest (prelude = prelude ):
398
+ check_syntax_error (self , prelude + "def func(x: (yield)): ..." , "yield expression cannot be used within an annotation" )
399
+ check_syntax_error (self , prelude + "def func(x: (yield from x)): ..." , "yield expression cannot be used within an annotation" )
400
+ check_syntax_error (self , prelude + "def func(x: (y := 3)): ..." , "named expression cannot be used within an annotation" )
401
+ check_syntax_error (self , prelude + "def func(x: (await 42)): ..." , "await expression cannot be used within an annotation" )
402
+ check_syntax_error (self , prelude + "def func(x: [y async for y in x]): ..." , "asynchronous comprehension outside of an asynchronous function" )
403
+ check_syntax_error (self , prelude + "def func(x: {y async for y in x}): ..." , "asynchronous comprehension outside of an asynchronous function" )
404
+ check_syntax_error (self , prelude + "def func(x: {y: y async for y in x}): ..." , "asynchronous comprehension outside of an asynchronous function" )
394
405
395
406
def test_no_exotic_expressions_in_unevaluated_annotations (self ):
396
407
preludes = [
@@ -406,6 +417,9 @@ def test_no_exotic_expressions_in_unevaluated_annotations(self):
406
417
check_syntax_error (self , prelude + "(x): (y := 3)" , "named expression cannot be used within an annotation" )
407
418
check_syntax_error (self , prelude + "(x): (__debug__ := 3)" , "named expression cannot be used within an annotation" )
408
419
check_syntax_error (self , prelude + "(x): (await 42)" , "await expression cannot be used within an annotation" )
420
+ check_syntax_error (self , prelude + "(x): [y async for y in x]" , "asynchronous comprehension outside of an asynchronous function" )
421
+ check_syntax_error (self , prelude + "(x): {y async for y in x}" , "asynchronous comprehension outside of an asynchronous function" )
422
+ check_syntax_error (self , prelude + "(x): {y: y async for y in x}" , "asynchronous comprehension outside of an asynchronous function" )
409
423
410
424
def test_ignore_non_simple_annotations (self ):
411
425
ns = run_code ("class X: (y): int" )
0 commit comments