mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
annotations: Add tests to check that async comprehensions produce errors (#132513)
This already works correctly but I forgot to test for it.
This commit is contained in:
parent
132b6bc98f
commit
70b322d313
1 changed files with 18 additions and 4 deletions
|
@ -387,10 +387,21 @@ class DeferredEvaluationTests(unittest.TestCase):
|
|||
self.assertEqual(Outer.__annotations__, {"x": Outer.Nested})
|
||||
|
||||
def test_no_exotic_expressions(self):
|
||||
check_syntax_error(self, "def func(x: (yield)): ...", "yield expression cannot be used within an annotation")
|
||||
check_syntax_error(self, "def func(x: (yield from x)): ...", "yield expression cannot be used within an annotation")
|
||||
check_syntax_error(self, "def func(x: (y := 3)): ...", "named expression cannot be used within an annotation")
|
||||
check_syntax_error(self, "def func(x: (await 42)): ...", "await expression cannot be used within an annotation")
|
||||
preludes = [
|
||||
"",
|
||||
"class X:\n ",
|
||||
"def f():\n ",
|
||||
"async def f():\n ",
|
||||
]
|
||||
for prelude in preludes:
|
||||
with self.subTest(prelude=prelude):
|
||||
check_syntax_error(self, prelude + "def func(x: (yield)): ...", "yield expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "def func(x: (yield from x)): ...", "yield expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "def func(x: (y := 3)): ...", "named expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "def func(x: (await 42)): ...", "await expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "def func(x: [y async for y in x]): ...", "asynchronous comprehension outside of an asynchronous function")
|
||||
check_syntax_error(self, prelude + "def func(x: {y async for y in x}): ...", "asynchronous comprehension outside of an asynchronous function")
|
||||
check_syntax_error(self, prelude + "def func(x: {y: y async for y in x}): ...", "asynchronous comprehension outside of an asynchronous function")
|
||||
|
||||
def test_no_exotic_expressions_in_unevaluated_annotations(self):
|
||||
preludes = [
|
||||
|
@ -406,6 +417,9 @@ class DeferredEvaluationTests(unittest.TestCase):
|
|||
check_syntax_error(self, prelude + "(x): (y := 3)", "named expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "(x): (__debug__ := 3)", "named expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "(x): (await 42)", "await expression cannot be used within an annotation")
|
||||
check_syntax_error(self, prelude + "(x): [y async for y in x]", "asynchronous comprehension outside of an asynchronous function")
|
||||
check_syntax_error(self, prelude + "(x): {y async for y in x}", "asynchronous comprehension outside of an asynchronous function")
|
||||
check_syntax_error(self, prelude + "(x): {y: y async for y in x}", "asynchronous comprehension outside of an asynchronous function")
|
||||
|
||||
def test_ignore_non_simple_annotations(self):
|
||||
ns = run_code("class X: (y): int")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue