Skip to content

Commit 19b5730

Browse files
authored
pythongh-141174: Improve ForwardRef test coverage (python#141175)
* Test unsupported format in ForwardRef.evaluate() * Test dict cell closure with multiple variables * Test all options in ForwardRef repr * Test ForwardRef being a final class
1 parent 68266c1 commit 19b5730

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Lib/test/test_annotationlib.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ def inner(arg: x):
7474
anno = get_annotations(inner, format=Format.FORWARDREF)
7575
self.assertEqual(anno["arg"], x)
7676

77+
def test_multiple_closure(self):
78+
def inner(arg: x[y]):
79+
pass
80+
81+
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
82+
self.assertIsInstance(fwdref, ForwardRef)
83+
self.assertEqual(fwdref.__forward_arg__, "x[y]")
84+
with self.assertRaises(NameError):
85+
fwdref.evaluate()
86+
87+
y = str
88+
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
89+
self.assertIsInstance(fwdref, ForwardRef)
90+
extra_name, extra_val = next(iter(fwdref.__extra_names__.items()))
91+
self.assertEqual(fwdref.__forward_arg__.replace(extra_name, extra_val.__name__), "x[str]")
92+
with self.assertRaises(NameError):
93+
fwdref.evaluate()
94+
95+
x = list
96+
self.assertEqual(fwdref.evaluate(), x[y])
97+
98+
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
99+
self.assertEqual(fwdref, x[y])
100+
77101
def test_function(self):
78102
def f(x: int, y: doesntexist):
79103
pass
@@ -1756,6 +1780,14 @@ def test_forward_repr(self):
17561780
repr(List[ForwardRef("int", module="mod")]),
17571781
"typing.List[ForwardRef('int', module='mod')]",
17581782
)
1783+
self.assertEqual(
1784+
repr(List[ForwardRef("int", module="mod", is_class=True)]),
1785+
"typing.List[ForwardRef('int', module='mod', is_class=True)]",
1786+
)
1787+
self.assertEqual(
1788+
repr(List[ForwardRef("int", owner="class")]),
1789+
"typing.List[ForwardRef('int', owner='class')]",
1790+
)
17591791

17601792
def test_forward_recursion_actually(self):
17611793
def namespace1():
@@ -1861,6 +1893,19 @@ def test_evaluate_forwardref_format(self):
18611893
support.EqualToForwardRef('"a" + 1'),
18621894
)
18631895

1896+
def test_evaluate_notimplemented_format(self):
1897+
class C:
1898+
x: alias
1899+
1900+
fwdref = get_annotations(C, format=Format.FORWARDREF)["x"]
1901+
1902+
with self.assertRaises(NotImplementedError):
1903+
fwdref.evaluate(format=Format.VALUE_WITH_FAKE_GLOBALS)
1904+
1905+
with self.assertRaises(NotImplementedError):
1906+
# Some other unsupported value
1907+
fwdref.evaluate(format=7)
1908+
18641909
def test_evaluate_with_type_params(self):
18651910
class Gen[T]:
18661911
alias = int
@@ -1994,6 +2039,11 @@ def test_fwdref_invalid_syntax(self):
19942039
with self.assertRaises(SyntaxError):
19952040
fr.evaluate()
19962041

2042+
def test_fwdref_final_class(self):
2043+
with self.assertRaises(TypeError):
2044+
class C(ForwardRef):
2045+
pass
2046+
19972047

19982048
class TestAnnotationLib(unittest.TestCase):
19992049
def test__all__(self):

0 commit comments

Comments
 (0)