summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-06-05 13:07:17 (GMT)
committerGitHub <noreply@github.com>2023-06-05 13:07:17 (GMT)
commit69d1245685cf95ddc678633e978a56673da64865 (patch)
tree2d3851bcde83d59a0b47f560380f62080eb8d9ad /Lib
parent06893403668961fdbd5d9ece18162eb3470fc8dd (diff)
downloadcpython-69d1245685cf95ddc678633e978a56673da64865.zip
cpython-69d1245685cf95ddc678633e978a56673da64865.tar.gz
cpython-69d1245685cf95ddc678633e978a56673da64865.tar.bz2
gh-105164: Detect annotations inside match blocks (#105177)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_type_annotations.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py
index 87f46c2c..3dbb35a 100644
--- a/Lib/test/test_type_annotations.py
+++ b/Lib/test/test_type_annotations.py
@@ -1,4 +1,6 @@
+import textwrap
import unittest
+from test.support import run_code
class TypeAnnotationTests(unittest.TestCase):
@@ -101,3 +103,112 @@ class TypeAnnotationTests(unittest.TestCase):
with self.assertRaises(AttributeError):
del D.__annotations__
self.assertEqual(D.__annotations__, {})
+
+
+class TestSetupAnnotations(unittest.TestCase):
+ def check(self, code: str):
+ code = textwrap.dedent(code)
+ for scope in ("module", "class"):
+ with self.subTest(scope=scope):
+ if scope == "class":
+ code = f"class C:\n{textwrap.indent(code, ' ')}"
+ ns = run_code(code)
+ if scope == "class":
+ annotations = ns["C"].__annotations__
+ else:
+ annotations = ns["__annotations__"]
+ self.assertEqual(annotations, {"x": int})
+
+ def test_top_level(self):
+ self.check("x: int = 1")
+
+ def test_blocks(self):
+ self.check("if True:\n x: int = 1")
+ self.check("""
+ while True:
+ x: int = 1
+ break
+ """)
+ self.check("""
+ while False:
+ pass
+ else:
+ x: int = 1
+ """)
+ self.check("""
+ for i in range(1):
+ x: int = 1
+ """)
+ self.check("""
+ for i in range(1):
+ pass
+ else:
+ x: int = 1
+ """)
+
+ def test_try(self):
+ self.check("""
+ try:
+ x: int = 1
+ except:
+ pass
+ """)
+ self.check("""
+ try:
+ pass
+ except:
+ pass
+ else:
+ x: int = 1
+ """)
+ self.check("""
+ try:
+ pass
+ except:
+ pass
+ finally:
+ x: int = 1
+ """)
+ self.check("""
+ try:
+ 1/0
+ except:
+ x: int = 1
+ """)
+
+ def test_try_star(self):
+ self.check("""
+ try:
+ x: int = 1
+ except* Exception:
+ pass
+ """)
+ self.check("""
+ try:
+ pass
+ except* Exception:
+ pass
+ else:
+ x: int = 1
+ """)
+ self.check("""
+ try:
+ pass
+ except* Exception:
+ pass
+ finally:
+ x: int = 1
+ """)
+ self.check("""
+ try:
+ 1/0
+ except* Exception:
+ x: int = 1
+ """)
+
+ def test_match(self):
+ self.check("""
+ match 0:
+ case 0:
+ x: int = 1
+ """)