diff options
author | kj <28750310+Fidget-Spinner@users.noreply.github.com> | 2020-12-24 04:33:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-24 04:33:48 (GMT) |
commit | 73607be68668ab7f4bee53507c8dc7b5a46c9cb4 (patch) | |
tree | 4ec6bb3f178e61b7fc61d979ec65519a4ef1d4bc /Lib/_collections_abc.py | |
parent | cc3467a57b61b0e7ef254b36790a1c44b13f2228 (diff) | |
download | cpython-73607be68668ab7f4bee53507c8dc7b5a46c9cb4.zip cpython-73607be68668ab7f4bee53507c8dc7b5a46c9cb4.tar.gz cpython-73607be68668ab7f4bee53507c8dc7b5a46c9cb4.tar.bz2 |
bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing (#23702)
Diffstat (limited to 'Lib/_collections_abc.py')
-rw-r--r-- | Lib/_collections_abc.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index e4eac79..87302ac 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -416,7 +416,7 @@ class Collection(Sized, Iterable, Container): class _CallableGenericAlias(GenericAlias): """ Represent `Callable[argtypes, resulttype]`. - This sets ``__args__`` to a tuple containing the flattened``argtypes`` + This sets ``__args__`` to a tuple containing the flattened ``argtypes`` followed by ``resulttype``. Example: ``Callable[[int, str], float]`` sets ``__args__`` to @@ -444,7 +444,7 @@ class _CallableGenericAlias(GenericAlias): return super().__new__(cls, origin, ga_args) def __repr__(self): - if len(self.__args__) == 2 and self.__args__[0] is Ellipsis: + if _has_special_args(self.__args__): return super().__repr__() return (f'collections.abc.Callable' f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], ' @@ -452,7 +452,7 @@ class _CallableGenericAlias(GenericAlias): def __reduce__(self): args = self.__args__ - if not (len(args) == 2 and args[0] is Ellipsis): + if not _has_special_args(args): args = list(args[:-1]), args[-1] return _CallableGenericAlias, (Callable, args) @@ -461,12 +461,28 @@ class _CallableGenericAlias(GenericAlias): # rather than the default types.GenericAlias object. ga = super().__getitem__(item) args = ga.__args__ - t_result = args[-1] - t_args = args[:-1] - args = (t_args, t_result) + # args[0] occurs due to things like Z[[int, str, bool]] from PEP 612 + if not isinstance(ga.__args__[0], tuple): + t_result = ga.__args__[-1] + t_args = ga.__args__[:-1] + args = (t_args, t_result) return _CallableGenericAlias(Callable, args) +def _has_special_args(args): + """Checks if args[0] matches either ``...``, ``ParamSpec`` or + ``_ConcatenateGenericAlias`` from typing.py + """ + if len(args) != 2: + return False + obj = args[0] + if obj is Ellipsis: + return True + obj = type(obj) + names = ('ParamSpec', '_ConcatenateGenericAlias') + return obj.__module__ == 'typing' and any(obj.__name__ == name for name in names) + + def _type_repr(obj): """Return the repr() of an object, special-casing types (internal helper). |