Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Loading
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
18 changes: 18 additions & 0 deletions 18 Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7178,6 +7178,24 @@ def func(x: undefined) -> undefined: ...
self.assertEqual(get_type_hints(func, format=annotationlib.Format.STRING),
{'x': 'undefined', 'return': 'undefined'})

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 26 Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,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 @@ -468,25 +479,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 @@ -2400,7 +2406,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
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.