Skip to content
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

gh-91621: Fix typing.get_type_hints for collections.abc.Callable #91656

Merged
merged 5 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4408,6 +4408,17 @@ def test_get_type_hints_typeddict(self):
'a': Annotated[Required[int], "a", "b", "c"]
})

def test_get_type_hints_collections_abc_callable(self):
# https://github.com/python/cpython/issues/91621
P = ParamSpec('P')
def f(x: collections.abc.Callable[[int], int]): ...
def g(x: collections.abc.Callable[..., int]): ...
def h(x: collections.abc.Callable[P, int]): ...

self.assertEqual(get_type_hints(f), {'x': collections.abc.Callable[[int], int]})
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(get_type_hints(g), {'x': collections.abc.Callable[..., int]})
self.assertEqual(get_type_hints(h), {'x': collections.abc.Callable[P, int]})


class GetUtilitiesTestCase(TestCase):
def test_get_origin(self):
Expand Down
6 changes: 5 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
ForwardRef(arg) if isinstance(arg, str) else arg
for arg in t.__args__
)
t = t.__origin__[args]
if (t.__origin__ is collections.abc.Callable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this code pattern appears once in Generic code and once here now. Should we just convert this into one function? Maybe _should_flatten_callable_args()? Seems abit tiresome to repeat this (and it's also confusing for people who haven't dealt with Callable's args before).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(sorry, missed this comment)
I added a helper function for this

and not (len(args) == 2 and _is_param_expr(args[0]))):
t = t.__origin__[(args[:-1], args[-1])]
else:
t = t.__origin__[args]
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
if ev_args == t.__args__:
return t
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`typing.get_type_hints` for :class:`collections.abc.Callable`. Patch by Shantanu Jain.