Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 70b322d

Browse filesBrowse files
annotations: Add tests to check that async comprehensions produce errors (#132513)
This already works correctly but I forgot to test for it.
1 parent 132b6bc commit 70b322d
Copy full SHA for 70b322d

File tree

Expand file treeCollapse file tree

1 file changed

+18
-4
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-4
lines changed

‎Lib/test/test_type_annotations.py

Copy file name to clipboardExpand all lines: Lib/test/test_type_annotations.py
+18-4Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,21 @@ class Nested: ...
387387
self.assertEqual(Outer.__annotations__, {"x": Outer.Nested})
388388

389389
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")
394405

395406
def test_no_exotic_expressions_in_unevaluated_annotations(self):
396407
preludes = [
@@ -406,6 +417,9 @@ def test_no_exotic_expressions_in_unevaluated_annotations(self):
406417
check_syntax_error(self, prelude + "(x): (y := 3)", "named expression cannot be used within an annotation")
407418
check_syntax_error(self, prelude + "(x): (__debug__ := 3)", "named expression cannot be used within an annotation")
408419
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")
409423

410424
def test_ignore_non_simple_annotations(self):
411425
ns = run_code("class X: (y): int")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.