From 6292be7adf247589bbf03524f8883cb4cb61f3e9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 27 Apr 2020 10:27:21 +0300 Subject: [PATCH] bpo-40398: Fix typing.get_args() for special generic aliases. (GH-19720) --- Lib/test/test_typing.py | 9 +++++++-- Lib/typing.py | 4 +++- .../Library/2020-04-26-22-25-36.bpo-40398.OdXnR3.rst | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-04-26-22-25-36.bpo-40398.OdXnR3.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 46e0ea559dd..f191d3bb9e9 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3007,6 +3007,8 @@ class GetUtilitiesTestCase(TestCase): self.assertIs(get_origin(List[Tuple[T, T]][int]), list) self.assertIs(get_origin(Annotated[T, 'thing']), Annotated) 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), None) @@ -3024,13 +3026,16 @@ class GetUtilitiesTestCase(TestCase): (int, Tuple[str, int])) self.assertEqual(get_args(typing.Dict[int, Tuple[T, T]][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]]), (int, Callable[[Tuple[T, ...]], str])) self.assertEqual(get_args(Tuple[int, ...]), (int, ...)) self.assertEqual(get_args(Tuple[()]), ((),)) 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), ()) diff --git a/Lib/typing.py b/Lib/typing.py index 1aefcb8a8a2..c8298986192 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1420,11 +1420,13 @@ def get_args(tp): """ if isinstance(tp, _AnnotatedAlias): return (tp.__origin__,) + tp.__metadata__ - if isinstance(tp, (_GenericAlias, GenericAlias)): + if isinstance(tp, _GenericAlias) and not tp._special: 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 () diff --git a/Misc/NEWS.d/next/Library/2020-04-26-22-25-36.bpo-40398.OdXnR3.rst b/Misc/NEWS.d/next/Library/2020-04-26-22-25-36.bpo-40398.OdXnR3.rst new file mode 100644 index 00000000000..a56da0c1095 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-04-26-22-25-36.bpo-40398.OdXnR3.rst @@ -0,0 +1,2 @@ +:func:`typing.get_args` now always returns an empty tuple for special +generic aliases.