Skip to content

gh-137706: make typing._is_unpacked_typevartuple check for True instead of truthy #137712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9788,6 +9788,19 @@ class B(str): ...
self.assertIs(type(field_c2.__metadata__[0]), float)
self.assertIs(type(field_c3.__metadata__[0]), bool)

def test_forwardref_partial_evaluation(self):
# Test that Annotated partially evaluates if it contains a ForwardRef
# See: https://github.com/python/cpython/issues/137706
def f(x: Annotated[undefined, '']): pass

ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)

# Test that the attributes are retrievable from the partially evaluated annotation
x_ann = ann['x']
self.assertIs(get_origin(x_ann), Annotated)
self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
self.assertEqual(x_ann.__metadata__, ('',))


class TypeAliasTests(BaseTestCase):
def test_canonical_usage_with_variable_annotation(self):
Expand Down
4 changes: 3 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,10 @@ def evaluate_forward_ref(


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


def _is_typevar_like(x: Any) -> bool:
Expand Down
Loading