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

Merged
merged 3 commits into from
Jul 5, 2025

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

@JelleZijlstra
Copy link
Member

This generally makes sense to me, though there's a series of issues in CI. I think this can only go into 3.15 at this point.

@ZeroIntensity
Copy link
Member

Yeah, this feels like a feature to me.

@Viicos
Copy link
Contributor Author

Viicos commented Jun 9, 2025

I would rather consider this as a bug fix but the risk of regressions is relatively high so it also makes sense to me to include this in 3.15 only as well.

@JelleZijlstra JelleZijlstra merged commit 5b56daa into python:main Jul 5, 2025
43 checks passed
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy