Skip to content

Commit 300c177

Browse files
DavidCEllismiss-islington
authored andcommitted
pythongh-137706: make typing._is_unpacked_typevartuple check for True instead of truthy (pythonGH-137712)
(cherry picked from commit 7e652f4) Co-authored-by: David Ellis <[email protected]>
1 parent f153e32 commit 300c177

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/test/test_typing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9811,6 +9811,19 @@ class B(str): ...
98119811
self.assertIs(type(field_c2.__metadata__[0]), float)
98129812
self.assertIs(type(field_c3.__metadata__[0]), bool)
98139813

9814+
def test_forwardref_partial_evaluation(self):
9815+
# Test that Annotated partially evaluates if it contains a ForwardRef
9816+
# See: https://github.com/python/cpython/issues/137706
9817+
def f(x: Annotated[undefined, '']): pass
9818+
9819+
ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)
9820+
9821+
# Test that the attributes are retrievable from the partially evaluated annotation
9822+
x_ann = ann['x']
9823+
self.assertIs(get_origin(x_ann), Annotated)
9824+
self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
9825+
self.assertEqual(x_ann.__metadata__, ('',))
9826+
98149827

98159828
class TypeAliasTests(BaseTestCase):
98169829
def test_canonical_usage_with_variable_annotation(self):

Lib/typing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,10 @@ def evaluate_forward_ref(
10111011

10121012

10131013
def _is_unpacked_typevartuple(x: Any) -> bool:
1014+
# Need to check 'is True' here
1015+
# See: https://github.com/python/cpython/issues/137706
10141016
return ((not isinstance(x, type)) and
1015-
getattr(x, '__typing_is_unpacked_typevartuple__', False))
1017+
getattr(x, '__typing_is_unpacked_typevartuple__', False) is True)
10161018

10171019

10181020
def _is_typevar_like(x: Any) -> bool:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the partial evaluation of annotations that use ``typing.Annotated[T, x]`` where ``T`` is a forward reference.

0 commit comments

Comments
 (0)