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 all commits
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
28 changes: 22 additions & 6 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def __subclasshook__(cls, C):
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
Expand Down Expand Up @@ -444,15 +444,15 @@ def __create_ga(cls, origin, args):
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]])}], '
f'{_type_repr(self.__args__[-1])}]')

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)

Expand All @@ -461,12 +461,28 @@ def __getitem__(self, item):
# 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).

Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,27 @@ def __call__(self):
self.assertEqual(c1.__args__, c2.__args__)
self.assertEqual(hash(c1.__args__), hash(c2.__args__))

with self.subTest("Testing ParamSpec uses"):
P = typing.ParamSpec('P')
C1 = Callable[P, T]
# 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')
C1 = Callable[typing.Concatenate[int, P], int]
self.assertEqual(repr(C1), "collections.abc.Callable"
"[typing.Concatenate[int, ~P], int]")

if __name__ == "__main__":
unittest.main()
120 changes: 106 additions & 14 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from typing import Pattern, Match
from typing import Annotated, ForwardRef
from typing import TypeAlias
from typing import ParamSpec, Concatenate
import abc
import typing
import weakref
Expand Down Expand Up @@ -1130,10 +1131,6 @@ class P(PR[int, T], Protocol[T]):
PR[int]
with self.assertRaises(TypeError):
P[int, str]
with self.assertRaises(TypeError):
PR[int, 1]
with self.assertRaises(TypeError):
PR[int, ClassVar]

class C(PR[int, T]): pass

Expand All @@ -1155,8 +1152,6 @@ class P(PR[int, str], Protocol):
self.assertIsSubclass(P, PR)
with self.assertRaises(TypeError):
PR[int]
with self.assertRaises(TypeError):
PR[int, 1]

class P1(Protocol, Generic[T]):
def bar(self, x: T) -> str: ...
Expand All @@ -1175,8 +1170,6 @@ def bar(self, x: str) -> str:
return x

self.assertIsInstance(Test(), PSub)
with self.assertRaises(TypeError):
PR[int, ClassVar]

def test_init_called(self):
T = TypeVar('T')
Expand Down Expand Up @@ -1746,8 +1739,6 @@ def test_extended_generic_rules_eq(self):
self.assertEqual(typing.Iterable[Tuple[T, T]][T], typing.Iterable[Tuple[T, T]])
with self.assertRaises(TypeError):
Tuple[T, int][()]
with self.assertRaises(TypeError):
Tuple[T, U][T, ...]

self.assertEqual(Union[T, int][int], int)
self.assertEqual(Union[T, U][int, Union[int, str]], Union[int, str])
Expand All @@ -1759,10 +1750,6 @@ class Derived(Base): ...

self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT])
self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
with self.assertRaises(TypeError):
Callable[[T], U][..., int]
with self.assertRaises(TypeError):
Callable[[T], U][[], int]

def test_extended_generic_rules_repr(self):
T = TypeVar('T')
Expand Down Expand Up @@ -4243,6 +4230,111 @@ def test_cannot_subscript(self):
TypeAlias[int]


class ParamSpecTests(BaseTestCase):

def test_basic_plain(self):
P = ParamSpec('P')
self.assertEqual(P, P)
self.assertIsInstance(P, ParamSpec)

def test_valid_uses(self):
P = ParamSpec('P')
T = TypeVar('T')
C1 = Callable[P, int]
self.assertEqual(C1.__args__, (P, int))
self.assertEqual(C1.__parameters__, (P,))
C2 = Callable[P, T]
self.assertEqual(C2.__args__, (P, T))
self.assertEqual(C2.__parameters__, (P, T))
# Test collections.abc.Callable too.
C3 = collections.abc.Callable[P, int]
self.assertEqual(C3.__args__, (P, int))
self.assertEqual(C3.__parameters__, (P,))
C4 = collections.abc.Callable[P, T]
self.assertEqual(C4.__args__, (P, T))
self.assertEqual(C4.__parameters__, (P, T))

# ParamSpec instances should also have args and kwargs attributes.
self.assertIn('args', dir(P))
self.assertIn('kwargs', dir(P))
P.args
P.kwargs

def test_user_generics(self):
T = TypeVar("T")
P = ParamSpec("P")
P_2 = ParamSpec("P_2")

class X(Generic[T, P]):
f: Callable[P, int]
x: T
G1 = X[int, P_2]
self.assertEqual(G1.__args__, (int, P_2))
self.assertEqual(G1.__parameters__, (P_2,))

G2 = X[int, Concatenate[int, P_2]]
self.assertEqual(G2.__args__, (int, Concatenate[int, P_2]))
self.assertEqual(G2.__parameters__, (P_2,))

G3 = X[int, [int, bool]]
self.assertEqual(G3.__args__, (int, (int, bool)))
self.assertEqual(G3.__parameters__, ())

G4 = X[int, ...]
self.assertEqual(G4.__args__, (int, Ellipsis))
self.assertEqual(G4.__parameters__, ())

class Z(Generic[P]):
f: Callable[P, int]

G5 = Z[[int, str, bool]]
self.assertEqual(G5.__args__, ((int, str, bool),))
self.assertEqual(G5.__parameters__, ())

G6 = Z[int, str, bool]
self.assertEqual(G6.__args__, ((int, str, bool),))
self.assertEqual(G6.__parameters__, ())

# G5 and G6 should be equivalent according to the PEP
self.assertEqual(G5.__args__, G6.__args__)
self.assertEqual(G5.__origin__, G6.__origin__)
self.assertEqual(G5.__parameters__, G6.__parameters__)
self.assertEqual(G5, G6)

def test_var_substitution(self):
T = TypeVar("T")
P = ParamSpec("P")
C1 = Callable[P, T]
self.assertEqual(C1[int, str], Callable[[int], str])
self.assertEqual(C1[[int, str, dict], float], Callable[[int, str, dict], float])


class ConcatenateTests(BaseTestCase):
def test_basics(self):
P = ParamSpec('P')
class MyClass: ...
c = Concatenate[MyClass, P]
self.assertNotEqual(c, Concatenate)

def test_valid_uses(self):
P = ParamSpec('P')
T = TypeVar('T')
C1 = Callable[Concatenate[int, P], int]
self.assertEqual(C1.__args__, (Concatenate[int, P], int))
self.assertEqual(C1.__parameters__, (P,))
C2 = Callable[Concatenate[int, T, P], T]
self.assertEqual(C2.__args__, (Concatenate[int, T, P], T))
self.assertEqual(C2.__parameters__, (T, P))

# Test collections.abc.Callable too.
C3 = collections.abc.Callable[Concatenate[int, P], int]
self.assertEqual(C3.__args__, (Concatenate[int, P], int))
self.assertEqual(C3.__parameters__, (P,))
C4 = collections.abc.Callable[Concatenate[int, T, P], T]
self.assertEqual(C4.__args__, (Concatenate[int, T, P], T))
self.assertEqual(C4.__parameters__, (T, P))


class AllTests(BaseTestCase):
"""Tests for __all__."""

Expand Down
Loading