From cdd2bd463434da7ee12d0641152a7a16236b4771 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sun, 27 Dec 2020 17:37:33 +0800 Subject: [PATCH 1/3] Fix get_args for PEP 585 Callables --- Lib/test/test_typing.py | 2 ++ Lib/typing.py | 4 +--- .../next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index c340c8a898289d..2299fc285975dd 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3048,6 +3048,8 @@ class C(Generic[T]): pass 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)) class CollectionsAbcTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index 7b79876d4ebc70..67e95dd78f34d4 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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 () diff --git a/Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst b/Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst new file mode 100644 index 00000000000000..931084fa606904 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst @@ -0,0 +1,2 @@ +:func:`typing.get_args` is now consistent between the parameterized generic +of ``collections.abc.Callable`` and ``typing.Callable``. From efbfcf48eb81740fa6b0fb9afeafa3c1c8a61d28 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sun, 27 Dec 2020 17:40:57 +0800 Subject: [PATCH 2/3] add more tests --- Lib/test/test_typing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 2299fc285975dd..3dcc31fe5be56c 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3050,6 +3050,9 @@ class C(Generic[T]): pass 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): From a011c4432e13b69f62b0476fb852ec3f32afa4c2 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 28 Dec 2020 00:22:18 +0800 Subject: [PATCH 3/3] Delete news entry --- .../next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst diff --git a/Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst b/Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst deleted file mode 100644 index 931084fa606904..00000000000000 --- a/Misc/NEWS.d/next/Library/2020-12-27-17-36-10.bpo-42740.vubNWp.rst +++ /dev/null @@ -1,2 +0,0 @@ -:func:`typing.get_args` is now consistent between the parameterized generic -of ``collections.abc.Callable`` and ``typing.Callable``.