Skip to content

gh-130870: Preserve GenericAlias subclasses in typing.get_type_hints() #131583

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

Conversation

Viicos
Copy link
Contributor

@Viicos Viicos commented Mar 22, 2025

This is an alternative approach to #130897, that avoids recreating GenericAlias instances two times (no noticeable performance improvement but cleans up the implementation).

It also fixes the GenericAlias reconstruction in _strip_annotations(), and as such I moved the logic into a separate function.

This approach has the benefit of not losing the __unpacked__ property of the GenericAlias if no forward annotation is present in the type hint. That is:

def func(*args: *tuple[int]): ...

get_type_hints(func)
# On main:
# {'args': typing.Unpack[tuple[int]]}
# On this branch:
# {'args': *tuple[int]} (that is, gth(func)['args'] is a the same `GenericAlias` instance, with `__unpacked__ = True`)

A couple tests are failing due to this, I'm not sure how we can test this? Maybe like:

def func2(*args: *tuple[int, str]): pass
self.assertEqual(gth(func2), func2.__annotations__)
self.assertTrue(gth(func2)['args'].__unpacked__)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant