bpo-42740: Fix get_args for PEP 585 collections.abc.Callable (GH-23963)

PR 1/2. Needs backport to 3.9.
This commit is contained in:
Ken Jin 2020-12-29 04:06:19 +08:00 committed by GitHub
parent a9621bb301
commit 4140f10a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -3048,6 +3048,11 @@ class GetUtilitiesTestCase(TestCase):
self.assertEqual(get_args(Callable), ())
self.assertEqual(get_args(list[int]), (int,))
self.assertEqual(get_args(list), ())
self.assertEqual(get_args(collections.abc.Callable[[int], str]), ([int], str))
self.assertEqual(get_args(collections.abc.Callable[..., str]), (..., str))
self.assertEqual(get_args(collections.abc.Callable[[], str]), ([], str))
self.assertEqual(get_args(collections.abc.Callable[[int], str]),
get_args(Callable[[int], str]))
class CollectionsAbcTests(BaseTestCase):

View File

@ -1684,13 +1684,11 @@ def get_args(tp):
"""
if isinstance(tp, _AnnotatedAlias):
return (tp.__origin__,) + tp.__metadata__
if isinstance(tp, _GenericAlias):
if isinstance(tp, (_GenericAlias, GenericAlias)):
res = tp.__args__
if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
res = (list(res[:-1]), res[-1])
return res
if isinstance(tp, GenericAlias):
return tp.__args__
return ()