Skip to content

Commit c472bce

Browse files
committed
Check for True instead of truthy for TypeVarTuple unpacking
truthy output was interpreting annotationlib _Stringifier instances as unpacked TypeVarTuples
1 parent 089a324 commit c472bce

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-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
@@ -1033,8 +1033,10 @@ def evaluate_forward_ref(
10331033

10341034

10351035
def _is_unpacked_typevartuple(x: Any) -> bool:
1036+
# Need to check 'is True' here
1037+
# See: https://github.com/python/cpython/issues/137706
10361038
return ((not isinstance(x, type)) and
1037-
getattr(x, '__typing_is_unpacked_typevartuple__', False))
1039+
getattr(x, '__typing_is_unpacked_typevartuple__', False) is True)
10381040

10391041

10401042
def _is_typevar_like(x: Any) -> bool:

0 commit comments

Comments
 (0)