Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdc02f83

Browse files
dr-carlosmiss-islington
authored andcommitted
gh-141174: ImproveForwardRef 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 commit19b5730)Co-authored-by: dr-carlos <77367421+dr-carlos@users.noreply.github.com>
1 parentc12a0be commitdc02f83

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
@@ -73,6 +73,30 @@ def inner(arg: x):
7373
anno=get_annotations(inner,format=Format.FORWARDREF)
7474
self.assertEqual(anno["arg"],x)
7575

76+
deftest_multiple_closure(self):
77+
definner(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+
withself.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+
withself.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
deftest_function(self):
77101
deff(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
deftest_forward_recursion_actually(self):
16931725
defnamespace1():
@@ -1793,6 +1825,19 @@ def test_evaluate_forwardref_format(self):
17931825
support.EqualToForwardRef('"a" + 1'),
17941826
)
17951827

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

1974+
deftest_fwdref_final_class(self):
1975+
withself.assertRaises(TypeError):
1976+
classC(ForwardRef):
1977+
pass
1978+
19291979

19301980
classTestAnnotationLib(unittest.TestCase):
19311981
deftest__all__(self):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp