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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-130870: Preserve GenericAlias subclasses in `typing.get_type_hin…
…ts()`
  • Loading branch information
Viicos committed Jun 10, 2025
commit abd9aedd66124303155fcfb9566891f05b275848
18 changes: 18 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7114,6 +7114,24 @@ def add_right(self, node: 'Node[T]' = None):
right_hints = get_type_hints(t.add_right, globals(), locals())
self.assertEqual(right_hints['node'], Node[T])

def test_get_type_hints_preserve_generic_alias_subclasses(self):
# https://github.com/python/cpython/issues/130870
# A real world example of this is `collections.abc.Callable`. When parameterized,
# the result is a subclass of `types.GenericAlias`.
class MyAlias(types.GenericAlias):
pass

class MyClass:
def __class_getitem__(cls, args):
return MyAlias(cls, args)

# Using a forward reference is important, otherwise it works as expected.
# `y` tests that the `GenericAlias` subclass is preserved when stripping `Annotated`.
def func(x: MyClass['int'], y: MyClass[Annotated[int, ...]]): ...

assert isinstance(get_type_hints(func)['x'], MyAlias)
assert isinstance(get_type_hints(func)['y'], MyAlias)


class GetUtilitiesTestCase(TestCase):
def test_get_origin(self):
Expand Down
26 changes: 16 additions & 10 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,17 @@ def inner(*args, **kwds):
return decorator


def _rebuild_generic_alias(alias: GenericAlias, args: tuple[object, ...]) -> GenericAlias:
is_unpacked = alias.__unpacked__
if _should_unflatten_callable_args(alias, args):
t = alias.__origin__[(args[:-1], args[-1])]
else:
t = alias.__origin__[args]
if is_unpacked:
t = Unpack[t]
return t


def _deprecation_warning_for_no_type_params_passed(funcname: str) -> None:
import warnings

Expand Down Expand Up @@ -454,25 +465,20 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
_make_forward_ref(arg) if isinstance(arg, str) else arg
for arg in t.__args__
)
is_unpacked = t.__unpacked__
if _should_unflatten_callable_args(t, args):
t = t.__origin__[(args[:-1], args[-1])]
else:
t = t.__origin__[args]
if is_unpacked:
t = Unpack[t]
else:
args = t.__args__

ev_args = tuple(
_eval_type(
a, globalns, localns, type_params, recursive_guard=recursive_guard,
format=format, owner=owner,
)
for a in t.__args__
for a in args
)
if ev_args == t.__args__:
return t
if isinstance(t, GenericAlias):
return GenericAlias(t.__origin__, ev_args)
return _rebuild_generic_alias(t, ev_args)
if isinstance(t, Union):
return functools.reduce(operator.or_, ev_args)
else:
Expand Down Expand Up @@ -2404,7 +2410,7 @@ def _strip_annotations(t):
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
if stripped_args == t.__args__:
return t
return GenericAlias(t.__origin__, stripped_args)
return _rebuild_generic_alias(t, stripped_args)
if isinstance(t, Union):
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
if stripped_args == t.__args__:
Expand Down
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