mirror of https://github.com/python/cpython
bpo-42740: Support PEP 604, 612 for typing.py get_args and get_origin (GH-23942)
This commit is contained in:
parent
a6d63a20df
commit
efb1f0918f
|
@ -3021,6 +3021,7 @@ class GetUtilitiesTestCase(TestCase):
|
|||
self.assertIs(get_origin(Callable), collections.abc.Callable)
|
||||
self.assertIs(get_origin(list[int]), list)
|
||||
self.assertIs(get_origin(list), None)
|
||||
self.assertIs(get_origin(list | str), types.Union)
|
||||
|
||||
def test_get_args(self):
|
||||
T = TypeVar('T')
|
||||
|
@ -3053,6 +3054,11 @@ class GetUtilitiesTestCase(TestCase):
|
|||
self.assertEqual(get_args(collections.abc.Callable[[], str]), ([], str))
|
||||
self.assertEqual(get_args(collections.abc.Callable[[int], str]),
|
||||
get_args(Callable[[int], str]))
|
||||
P = ParamSpec('P')
|
||||
self.assertEqual(get_args(Callable[P, int]), (P, int))
|
||||
self.assertEqual(get_args(Callable[Concatenate[int, P], int]),
|
||||
(Concatenate[int, P], int))
|
||||
self.assertEqual(get_args(list | str), (list, str))
|
||||
|
||||
|
||||
class CollectionsAbcTests(BaseTestCase):
|
||||
|
|
|
@ -1668,6 +1668,8 @@ def get_origin(tp):
|
|||
return tp.__origin__
|
||||
if tp is Generic:
|
||||
return Generic
|
||||
if isinstance(tp, types.Union):
|
||||
return types.Union
|
||||
return None
|
||||
|
||||
|
||||
|
@ -1686,9 +1688,13 @@ def get_args(tp):
|
|||
return (tp.__origin__,) + tp.__metadata__
|
||||
if isinstance(tp, (_GenericAlias, GenericAlias)):
|
||||
res = tp.__args__
|
||||
if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
|
||||
if (tp.__origin__ is collections.abc.Callable
|
||||
and not (res[0] is Ellipsis
|
||||
or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
|
||||
res = (list(res[:-1]), res[-1])
|
||||
return res
|
||||
if isinstance(tp, types.Union):
|
||||
return tp.__args__
|
||||
return ()
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
:func:`typing.get_args` and :func:`typing.get_origin` now support :pep:`604`
|
||||
union types and :pep:`612` additions to ``Callable``.
|
Loading…
Reference in New Issue