Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Jun 1, 2023
1 parent f7ed5ce commit 4d09ac4
Showing 1 changed file with 103 additions and 7 deletions.
110 changes: 103 additions & 7 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import textwrap
import unittest
from test.support import run_code

Expand Down Expand Up @@ -103,16 +104,111 @@ class D(metaclass=C):
del D.__annotations__
self.assertEqual(D.__annotations__, {})

def test_module_level(self):
ns = run_code("x: int = 1")
self.assertEqual(ns["__annotations__"], {"x": int})

ns = run_code("if True:\n x: int = 1")
self.assertEqual(ns["__annotations__"], {"x": int})
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
""")

ns = run_code("""
def test_match(self):
self.check("""
match 0:
case 0:
x: int = 1
""")
self.assertEqual(ns["__annotations__"], {"x": int})

0 comments on commit 4d09ac4

Please sign in to comment.