Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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")
Expand Down
Loading