https://github.com/python/cpython/commit/70b322d3138311df53bc9fc57f174c8e9bdc2ab5
commit: 70b322d3138311df53bc9fc57f174c8e9bdc2ab5
branch: main
author: Jelle Zijlstra <jelle.zijls...@gmail.com>
committer: JelleZijlstra <jelle.zijls...@gmail.com>
date: 2025-04-21T07:06:33-07:00
summary:

annotations: Add tests to check that async comprehensions produce errors 
(#132513)

This already works correctly but I forgot to test for it.

files:
M Lib/test/test_type_annotations.py

diff --git a/Lib/test/test_type_annotations.py 
b/Lib/test/test_type_annotations.py
index 31df7668db0976..f903f9bb51f919 100644
--- a/Lib/test/test_type_annotations.py
+++ b/Lib/test/test_type_annotations.py
@@ -387,10 +387,21 @@ class Nested: ...
         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 @@ def 
test_no_exotic_expressions_in_unevaluated_annotations(self):
                 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")

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to