Skip to content

Commit dc02f83

Browse files
dr-carlosmiss-islington
authored andcommitted
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 <77367421+dr-carlos@users.noreply.github.com>
1 parent c12a0be commit dc02f83

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎Lib/test/test_annotationlib.py‎

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

76+
def test_multiple_closure(self):
77+
def inner(arg: x[y]):
78+
pass
79+
80+
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
81+
self.assertIsInstance(fwdref, ForwardRef)
82+
self.assertEqual(fwdref.__forward_arg__, "x[y]")
83+
with self.assertRaises(NameError):
84+
fwdref.evaluate()
85+
86+
y = str
87+
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
88+
self.assertIsInstance(fwdref, ForwardRef)
89+
extra_name, extra_val = next(iter(fwdref.__extra_names__.items()))
90+
self.assertEqual(fwdref.__forward_arg__.replace(extra_name, extra_val.__name__), "x[str]")
91+
with self.assertRaises(NameError):
92+
fwdref.evaluate()
93+
94+
x = list
95+
self.assertEqual(fwdref.evaluate(), x[y])
96+
97+
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
98+
self.assertEqual(fwdref, x[y])
99+
76100
def test_function(self):
77101
def f(x: int, y: doesntexist):
78102
pass
@@ -1688,6 +1712,14 @@ def test_forward_repr(self):
16881712
repr(List[ForwardRef("int", module="mod")]),
16891713
"typing.List[ForwardRef('int', module='mod')]",
16901714
)
1715+
self.assertEqual(
1716+
repr(List[ForwardRef("int", module="mod", is_class=True)]),
1717+
"typing.List[ForwardRef('int', module='mod', is_class=True)]",
1718+
)
1719+
self.assertEqual(
1720+
repr(List[ForwardRef("int", owner="class")]),
1721+
"typing.List[ForwardRef('int', owner='class')]",
1722+
)
16911723

16921724
def test_forward_recursion_actually(self):
16931725
def namespace1():
@@ -1793,6 +1825,19 @@ def test_evaluate_forwardref_format(self):
17931825
support.EqualToForwardRef('"a" + 1'),
17941826
)
17951827

1828+
def test_evaluate_notimplemented_format(self):
1829+
class C:
1830+
x: alias
1831+
1832+
fwdref = get_annotations(C, format=Format.FORWARDREF)["x"]
1833+
1834+
with self.assertRaises(NotImplementedError):
1835+
fwdref.evaluate(format=Format.VALUE_WITH_FAKE_GLOBALS)
1836+
1837+
with self.assertRaises(NotImplementedError):
1838+
# Some other unsupported value
1839+
fwdref.evaluate(format=7)
1840+
17961841
def test_evaluate_with_type_params(self):
17971842
class Gen[T]:
17981843
alias = int
@@ -1926,6 +1971,11 @@ def test_fwdref_invalid_syntax(self):
19261971
with self.assertRaises(SyntaxError):
19271972
fr.evaluate()
19281973

1974+
def test_fwdref_final_class(self):
1975+
with self.assertRaises(TypeError):
1976+
class C(ForwardRef):
1977+
pass
1978+
19291979

19301980
class TestAnnotationLib(unittest.TestCase):
19311981
def test__all__(self):

0 commit comments

Comments
 (0)