mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-141174: Improve ForwardRef test coverage (#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
This commit is contained in:
parent
68266c1f01
commit
19b573025e
1 changed files with 50 additions and 0 deletions
|
|
@ -74,6 +74,30 @@ def inner(arg: x):
|
|||
anno = get_annotations(inner, format=Format.FORWARDREF)
|
||||
self.assertEqual(anno["arg"], x)
|
||||
|
||||
def test_multiple_closure(self):
|
||||
def inner(arg: x[y]):
|
||||
pass
|
||||
|
||||
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
|
||||
self.assertIsInstance(fwdref, ForwardRef)
|
||||
self.assertEqual(fwdref.__forward_arg__, "x[y]")
|
||||
with self.assertRaises(NameError):
|
||||
fwdref.evaluate()
|
||||
|
||||
y = str
|
||||
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
|
||||
self.assertIsInstance(fwdref, ForwardRef)
|
||||
extra_name, extra_val = next(iter(fwdref.__extra_names__.items()))
|
||||
self.assertEqual(fwdref.__forward_arg__.replace(extra_name, extra_val.__name__), "x[str]")
|
||||
with self.assertRaises(NameError):
|
||||
fwdref.evaluate()
|
||||
|
||||
x = list
|
||||
self.assertEqual(fwdref.evaluate(), x[y])
|
||||
|
||||
fwdref = get_annotations(inner, format=Format.FORWARDREF)["arg"]
|
||||
self.assertEqual(fwdref, x[y])
|
||||
|
||||
def test_function(self):
|
||||
def f(x: int, y: doesntexist):
|
||||
pass
|
||||
|
|
@ -1756,6 +1780,14 @@ def test_forward_repr(self):
|
|||
repr(List[ForwardRef("int", module="mod")]),
|
||||
"typing.List[ForwardRef('int', module='mod')]",
|
||||
)
|
||||
self.assertEqual(
|
||||
repr(List[ForwardRef("int", module="mod", is_class=True)]),
|
||||
"typing.List[ForwardRef('int', module='mod', is_class=True)]",
|
||||
)
|
||||
self.assertEqual(
|
||||
repr(List[ForwardRef("int", owner="class")]),
|
||||
"typing.List[ForwardRef('int', owner='class')]",
|
||||
)
|
||||
|
||||
def test_forward_recursion_actually(self):
|
||||
def namespace1():
|
||||
|
|
@ -1861,6 +1893,19 @@ def test_evaluate_forwardref_format(self):
|
|||
support.EqualToForwardRef('"a" + 1'),
|
||||
)
|
||||
|
||||
def test_evaluate_notimplemented_format(self):
|
||||
class C:
|
||||
x: alias
|
||||
|
||||
fwdref = get_annotations(C, format=Format.FORWARDREF)["x"]
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
fwdref.evaluate(format=Format.VALUE_WITH_FAKE_GLOBALS)
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
# Some other unsupported value
|
||||
fwdref.evaluate(format=7)
|
||||
|
||||
def test_evaluate_with_type_params(self):
|
||||
class Gen[T]:
|
||||
alias = int
|
||||
|
|
@ -1994,6 +2039,11 @@ def test_fwdref_invalid_syntax(self):
|
|||
with self.assertRaises(SyntaxError):
|
||||
fr.evaluate()
|
||||
|
||||
def test_fwdref_final_class(self):
|
||||
with self.assertRaises(TypeError):
|
||||
class C(ForwardRef):
|
||||
pass
|
||||
|
||||
|
||||
class TestAnnotationLib(unittest.TestCase):
|
||||
def test__all__(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue