Skip to content

Commit a188995

Browse files
[3.14] gh-141174: Improve ForwardRef test coverage (GH-141175) (#141354)
gh-141174: Improve `ForwardRef` test coverage (GH-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 (cherry picked from commit 19b5730) Co-authored-by: dr-carlos <[email protected]>
1 parent 4b9ed4e commit a188995

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
@@ -1799,6 +1823,14 @@ def test_forward_repr(self):
17991823
repr(List[ForwardRef("int", module="mod")]),
18001824
"typing.List[ForwardRef('int', module='mod')]",
18011825
)
1826+
self.assertEqual(
1827+
repr(List[ForwardRef("int", module="mod", is_class=True)]),
1828+
"typing.List[ForwardRef('int', module='mod', is_class=True)]",
1829+
)
1830+
self.assertEqual(
1831+
repr(List[ForwardRef("int", owner="class")]),
1832+
"typing.List[ForwardRef('int', owner='class')]",
1833+
)
18021834

18031835
def test_forward_recursion_actually(self):
18041836
def namespace1():
@@ -1904,6 +1936,19 @@ def test_evaluate_forwardref_format(self):
19041936
support.EqualToForwardRef('"a" + 1'),
19051937
)
19061938

1939+
def test_evaluate_notimplemented_format(self):
1940+
class C:
1941+
x: alias
1942+
1943+
fwdref = get_annotations(C, format=Format.FORWARDREF)["x"]
1944+
1945+
with self.assertRaises(NotImplementedError):
1946+
fwdref.evaluate(format=Format.VALUE_WITH_FAKE_GLOBALS)
1947+
1948+
with self.assertRaises(NotImplementedError):
1949+
# Some other unsupported value
1950+
fwdref.evaluate(format=7)
1951+
19071952
def test_evaluate_with_type_params(self):
19081953
class Gen[T]:
19091954
alias = int
@@ -2037,6 +2082,11 @@ def test_fwdref_invalid_syntax(self):
20372082
with self.assertRaises(SyntaxError):
20382083
fr.evaluate()
20392084

2085+
def test_fwdref_final_class(self):
2086+
with self.assertRaises(TypeError):
2087+
class C(ForwardRef):
2088+
pass
2089+
20402090

20412091
class TestAnnotationLib(unittest.TestCase):
20422092
def test__all__(self):

0 commit comments

Comments
 (0)