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

This mirrors logic in typing.get_args. The trickiness comes from how we
flatten args in collections.abc.Callable, see
https://bugs.python.org/issue42195
This commit is contained in:
Shantanu 2022-05-02 17:08:28 -06:00 committed by GitHub
parent aff8c4f488
commit ebb8b512e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View file

@ -4876,6 +4876,17 @@ class GetTypeHintTests(BaseTestCase):
'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]})
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):