bpo-40398: Fix typing.get_args() for special generic aliases. (GH-19720)

This commit is contained in:
Serhiy Storchaka 2020-04-27 10:27:21 +03:00 committed by GitHub
parent caf1aadf3d
commit 6292be7adf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 3 deletions

View File

@ -3007,6 +3007,8 @@ class GetUtilitiesTestCase(TestCase):
self.assertIs(get_origin(List[Tuple[T, T]][int]), list) self.assertIs(get_origin(List[Tuple[T, T]][int]), list)
self.assertIs(get_origin(Annotated[T, 'thing']), Annotated) self.assertIs(get_origin(Annotated[T, 'thing']), Annotated)
self.assertIs(get_origin(List), list) self.assertIs(get_origin(List), list)
self.assertIs(get_origin(Tuple), tuple)
self.assertIs(get_origin(Callable), collections.abc.Callable)
self.assertIs(get_origin(list[int]), list) self.assertIs(get_origin(list[int]), list)
self.assertIs(get_origin(list), None) self.assertIs(get_origin(list), None)
@ -3024,13 +3026,16 @@ class GetUtilitiesTestCase(TestCase):
(int, Tuple[str, int])) (int, Tuple[str, int]))
self.assertEqual(get_args(typing.Dict[int, Tuple[T, T]][Optional[int]]), self.assertEqual(get_args(typing.Dict[int, Tuple[T, T]][Optional[int]]),
(int, Tuple[Optional[int], Optional[int]])) (int, Tuple[Optional[int], Optional[int]]))
self.assertEqual(get_args(Callable[[], T][int]), ([], int,)) self.assertEqual(get_args(Callable[[], T][int]), ([], int))
self.assertEqual(get_args(Callable[..., int]), (..., int))
self.assertEqual(get_args(Union[int, Callable[[Tuple[T, ...]], str]]), self.assertEqual(get_args(Union[int, Callable[[Tuple[T, ...]], str]]),
(int, Callable[[Tuple[T, ...]], str])) (int, Callable[[Tuple[T, ...]], str]))
self.assertEqual(get_args(Tuple[int, ...]), (int, ...)) self.assertEqual(get_args(Tuple[int, ...]), (int, ...))
self.assertEqual(get_args(Tuple[()]), ((),)) self.assertEqual(get_args(Tuple[()]), ((),))
self.assertEqual(get_args(Annotated[T, 'one', 2, ['three']]), (T, 'one', 2, ['three'])) self.assertEqual(get_args(Annotated[T, 'one', 2, ['three']]), (T, 'one', 2, ['three']))
self.assertEqual(get_args(List), (typing.T,)) self.assertEqual(get_args(List), ())
self.assertEqual(get_args(Tuple), ())
self.assertEqual(get_args(Callable), ())
self.assertEqual(get_args(list[int]), (int,)) self.assertEqual(get_args(list[int]), (int,))
self.assertEqual(get_args(list), ()) self.assertEqual(get_args(list), ())

View File

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

View File

@ -0,0 +1,2 @@
:func:`typing.get_args` now always returns an empty tuple for special
generic aliases.