Skip to content

Commit 7e652f4

Browse files
authored
gh-137706: make typing._is_unpacked_typevartuple check for True instead of truthy (#137712)
1 parent bde1291 commit 7e652f4

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
@@ -9788,6 +9788,19 @@ class B(str): ...
97889788
self.assertIs(type(field_c2.__metadata__[0]), float)
97899789
self.assertIs(type(field_c3.__metadata__[0]), bool)
97909790

9791+
def test_forwardref_partial_evaluation(self):
9792+
# Test that Annotated partially evaluates if it contains a ForwardRef
9793+
# See: https://github.com/python/cpython/issues/137706
9794+
def f(x: Annotated[undefined, '']): pass
9795+
9796+
ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)
9797+
9798+
# Test that the attributes are retrievable from the partially evaluated annotation
9799+
x_ann = ann['x']
9800+
self.assertIs(get_origin(x_ann), Annotated)
9801+
self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
9802+
self.assertEqual(x_ann.__metadata__, ('',))
9803+
97919804

97929805
class TypeAliasTests(BaseTestCase):
97939806
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
@@ -1027,8 +1027,10 @@ def evaluate_forward_ref(
10271027

10281028

10291029
def _is_unpacked_typevartuple(x: Any) -> bool:
1030+
# Need to check 'is True' here
1031+
# See: https://github.com/python/cpython/issues/137706
10301032
return ((not isinstance(x, type)) and
1031-
getattr(x, '__typing_is_unpacked_typevartuple__', False))
1033+
getattr(x, '__typing_is_unpacked_typevartuple__', False) is True)
10321034

10331035

10341036
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)