Skip to content

Commit dd5d86c

Browse files
[3.14] gh-137706: make typing._is_unpacked_typevartuple check for True instead of truthy (GH-137712) (#138574)
gh-137706: make typing._is_unpacked_typevartuple check for `True` instead of truthy (GH-137712) (cherry picked from commit 7e652f4) Co-authored-by: David Ellis <ducksual@gmail.com>
1 parent ed0e63f commit dd5d86c

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
@@ -9851,6 +9851,19 @@ class B(str): ...
98519851
self.assertIs(type(field_c2.__metadata__[0]), float)
98529852
self.assertIs(type(field_c3.__metadata__[0]), bool)
98539853

9854+
def test_forwardref_partial_evaluation(self):
9855+
# Test that Annotated partially evaluates if it contains a ForwardRef
9856+
# See: https://github.com/python/cpython/issues/137706
9857+
def f(x: Annotated[undefined, '']): pass
9858+
9859+
ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)
9860+
9861+
# Test that the attributes are retrievable from the partially evaluated annotation
9862+
x_ann = ann['x']
9863+
self.assertIs(get_origin(x_ann), Annotated)
9864+
self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
9865+
self.assertEqual(x_ann.__metadata__, ('',))
9866+
98549867

98559868
class TypeAliasTests(BaseTestCase):
98569869
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
@@ -1024,8 +1024,10 @@ def evaluate_forward_ref(
10241024

10251025

10261026
def _is_unpacked_typevartuple(x: Any) -> bool:
1027+
# Need to check 'is True' here
1028+
# See: https://github.com/python/cpython/issues/137706
10271029
return ((not isinstance(x, type)) and
1028-
getattr(x, '__typing_is_unpacked_typevartuple__', False))
1030+
getattr(x, '__typing_is_unpacked_typevartuple__', False) is True)
10291031

10301032

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