8000 bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing by Fidget-Spinner · Pull Request #23702 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing #23702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
219b4ee
Add ParamSpec and Concatenate
Fidget-Spinner Dec 8, 2020
a1c0d0a
support ParamSpec in generics
Fidget-Spinner Dec 8, 2020
7b3beab
Add typing tests, disallow Concatenate in other types
Fidget-Spinner Dec 9, 2020
5dd3b44
Add news
Fidget-Spinner Dec 9, 2020
59c0b20
Address some of Guido's review comments
Fidget-Spinner Dec 10, 2020
4c381b3
remove extraneous empty lines
Fidget-Spinner Dec 10, 2020
b36b62d
Support ParamSpec in __parameters__ of typing and builtin GenericAlias
Fidget-Spinner Dec 10, 2020
d09d088
add tests for user defined generics
Fidget-Spinner Dec 10, 2020
0a19f34
Merge remote-tracking branch 'upstream/master' into pep612
Fidget-Spinner Dec 14, 2020
9727e2a
cast list to tuple done, loosened type checks for Generic
Fidget-Spinner Dec 14, 2020
cc7fc1c
loosen generics, allow typevar-like subst, flatten out args if Callable
Fidget-Spinner Dec 15, 2020
3e67f23
fix whitespace issue, cast list to tuple for types.GenericAlias
Fidget-Spinner Dec 15, 2020
d9baa1a
convert list to tuples if substituting paramspecs in types.GenericAlias
Fidget-Spinner Dec 16, 2020
c4155b6
done! flattened __args__ in substitutions for collections.abc.Callable
Fidget-Spinner Dec 16, 2020
2dbf861
fix repr problems, add repr tests
Fidget-Spinner Dec 16, 2020
87c2d19
Add another test for multiple chaining
Fidget-Spinner Dec 16, 2020
2b09de6
fix typo
Fidget-Spinner Dec 16, 2020
d980702
Clean up some comments
Fidget-Spinner Dec 17, 2020
45f7894
Merge remote-tracking branch 'upstream/master' into pep612
Fidget-Spinner Dec 22, 2020
d6f777c
remove stray whitespace
Fidget-Spinner Dec 22, 2020
9a8176b
Address nearly all of Guido's reviews
Fidget-Spinner Dec 23, 2020
fa06838
more reviews; fix some docstrings, clean up code, cast list to tuple …
Fidget-Spinner Dec 23, 2020
b8672cd
remove uneeded tests copied over from typevar
Fidget-Spinner Dec 23, 2020
51a463c
remove unused variable
Fidget-Spinner Dec 23, 2020
6d5b754
Merge remote-tracking branch 'upstream/master' into pep612
Fidget-Spinner Dec 24, 2020
c05d5d7
merge length checking into _has_special_args too
Fidget-Spinner Dec 24, 2020
c49ba30
Update Lib/_collections_abc.py
gvanrossum Dec 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix repr problems, add repr tests
  • Loading branch information
Fidget-Spinner committed Dec 16, 2020
commit 2dbf86145caaa9947c4db82e26e375f5041e793c
32 changes: 16 additions & 16 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def __create_ga(cls, origin, args):
raise TypeError(
"Callable must be used as Callable[[arg, ...], result].")
t_args, t_result = args
if isinstance(t_args, list):
if isinstance(t_args, (list, tuple)):
ga_args = tuple(t_args) + (t_result,)
# This relaxes what t_args can be on purpose to allow things like
# PEP 612 ParamSpec. Responsibility for whether a user is using
Expand All @@ -445,15 +445,16 @@ def __create_ga(cls, origin, args):

def __repr__(self):
if len(self.__args__) == 2 and (self.__args__[0] is Ellipsis
or _is_typing(self.__args__[0])):
or _concat_or_paramspec(self.__args__[0])):
return super().__repr__()
return (f'collections.abc.Callable'
f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
f'{_type_repr(self.__args__[-1])}]')

def __reduce__(self):
args = self.__args__
if not (len(args) == 2 and (args[0] is Ellipsis or _is_typing(args[0]))):
if not (len(args) == 2 and (args[0] is Ellipsis
or _concat_or_paramspec(args[0]))):
args = list(args[:-1]), args[-1]
return _CallableGenericAlias, (Callable, args)

Expand All @@ -463,22 +464,21 @@ def __getitem__(self, item):
# C1[[int, str], str] == Callable[[int, str], str]
# Where P is a PEP 612 ParamSpec.
ga = super().__getitem__(item)
new_args = []
# flatten args
if isinstance(ga.__args__[0], tuple):
new_args.extend(ga.__args__[0])
new_args.extend(ga.__args__[1:])
else:
new_args = ga.__args__
return GenericAlias(Callable, tuple(new_args))
args = ga.__args__
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 _is_typing(obj):
"""Checks if obj is from typing.py"""
if isinstance(obj, type):
return obj.__module__ == 'typing'
return type(obj).__module__ == 'typing'
def _is_typing_names(obj, names):
"""Checks if obj matches one of the names in *names* in typing.py"""
obj = type(obj)
return obj.__module__ == 'typing' and any(obj.__name__ == name for name in names)

def _concat_or_paramspec(obj):
return _is_typing_names(obj, ('ParamSpec', '_ConcatenateGenericAlias'))

def _type_repr(obj):
"""Return the repr() of an object, special-casing types (internal helper).
Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,21 @@ def __call__(self):
# substitution
self.assertEqual(C1[int, str], Callable[[int], str])
self.assertEqual(C1[[int, str], str], Callable[[int, str], str])
self.assertEqual(repr(C1).split(".")[-1], "Callable[~P, ~T]")
self.assertEqual(repr(C1[int, str]).split(".")[-1], "Callable[[int], str]")

C2 = Callable[P, int]
# special case in PEP 612 where
# X[int, str, float] == X[[int, str, float]]
self.assertEqual(C2[int, str, float], C2[[int, str, float]])
self.assertEqual(repr(C2).split(".")[-1], "Callable[~P, int]")
self.assertEqual(repr(C2[int, str]).split(".")[-1], "Callable[[int, str], int]")

with self.subTest("Testing Concatenate uses"):
P = typing.ParamSpec('P')
Callable[typing.Concatenate[int, P], int]

C1 = Callable[typing.Concatenate[int, P], int]
self.assertEqual(repr(C1), "collections.abc.Callable"
"[typing.Concatenate[int, ~P], int]")

if __name__ == "__main__":
unittest.main()
0