Skip to content

Commit 3875a69

Browse files
bpo-44801: Check arguments in substitution of ParamSpec in Callable (GH-27585)
1 parent 10faada commit 3875a69

File tree

4 files changed

+89
-37
lines changed

4 files changed

+89
-37
lines changed

Lib/_collections_abc.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -426,22 +426,16 @@ class _CallableGenericAlias(GenericAlias):
426426
__slots__ = ()
427427

428428
def __new__(cls, origin, args):
429-
return cls.__create_ga(origin, args)
430-
431-
@classmethod
432-
def __create_ga(cls, origin, args):
433-
if not isinstance(args, tuple) or len(args) != 2:
429+
if not (isinstance(args, tuple) and len(args) == 2):
434430
raise TypeError(
435431
"Callable must be used as Callable[[arg, ...], result].")
436432
t_args, t_result = args
437-
if isinstance(t_args, (list, tuple)):
438-
ga_args = tuple(t_args) + (t_result,)
439-
# This relaxes what t_args can be on purpose to allow things like
440-
# PEP 612 ParamSpec. Responsibility for whether a user is using
441-
# Callable[...] properly is deferred to static type checkers.
442-
else:
443-
ga_args = args
444-
return super().__new__(cls, origin, ga_args)
433+
if isinstance(t_args, list):
434+
args = (*t_args, t_result)
435+
elif not _is_param_expr(t_args):
436+
raise TypeError(f"Expected a list of types, an ellipsis, "
437+
f"ParamSpec, or Concatenate. Got {t_args}")
438+
return super().__new__(cls, origin, args)
445439

446440
@property
447441
def __parameters__(self):
@@ -456,15 +450,15 @@ def __parameters__(self):
456450
return tuple(dict.fromkeys(params))
457451

458452
def __repr__(self):
459-
if _has_special_args(self.__args__):
453+
if len(self.__args__) == 2 and _is_param_expr(self.__args__[0]):
460454
return super().__repr__()
461455
return (f'collections.abc.Callable'
462456
f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
463457
f'{_type_repr(self.__args__[-1])}]')
464458

465459
def __reduce__(self):
466460
args = self.__args__
467-
if not _has_special_args(args):
461+
if not (len(args) == 2 and _is_param_expr(args[0])):
468462
args = list(args[:-1]), args[-1]
469463
return _CallableGenericAlias, (Callable, args)
470464

@@ -479,10 +473,11 @@ def __getitem__(self, item):
479473
param_len = len(self.__parameters__)
480474
if param_len == 0:
481475
raise TypeError(f'{self} is not a generic class')
482-
if (param_len == 1
483-
and isinstance(item, (tuple, list))
484-
and len(item) > 1) or not isinstance(item, tuple):
476+
if not isinstance(item, tuple):
485477
item = (item,)
478+
if (param_len == 1 and _is_param_expr(self.__parameters__[0])
479+
and item and not _is_param_expr(item[0])):
480+
item = (list(item),)
486481
item_len = len(item)
487482
if item_len != param_len:
488483
raise TypeError(f'Too {"many" if item_len > param_len else "few"}'
@@ -492,7 +487,13 @@ def __getitem__(self, item):
492487
new_args = []
493488
for arg in self.__args__:
494489
if _is_typevarlike(arg):
495-
arg = subst[arg]
490+
if _is_param_expr(arg):
491+
arg = subst[arg]
492+
if not _is_param_expr(arg):
493+
raise TypeError(f"Expected a list of types, an ellipsis, "
494+
f"ParamSpec, or Concatenate. Got {arg}")
495+
else:
496+
arg = subst[arg]
496497
# Looks like a GenericAlias
497498
elif hasattr(arg, '__parameters__') and isinstance(arg.__parameters__, tuple):
498499
subparams = arg.__parameters__
@@ -502,32 +503,31 @@ def __getitem__(self, item):
502503
new_args.append(arg)
503504

504505
# args[0] occurs due to things like Z[[int, str, bool]] from PEP 612
505-
if not isinstance(new_args[0], (tuple, list)):
506+
if not isinstance(new_args[0], list):
506507
t_result = new_args[-1]
507508
t_args = new_args[:-1]
508509
new_args = (t_args, t_result)
509510
return _CallableGenericAlias(Callable, tuple(new_args))
510511

512+
511513
def _is_typevarlike(arg):
512514
obj = type(arg)
513515
# looks like a TypeVar/ParamSpec
514516
return (obj.__module__ == 'typing'
515517
and obj.__name__ in {'ParamSpec', 'TypeVar'})
516518

517-
def _has_special_args(args):
518-
"""Checks if args[0] matches either ``...``, ``ParamSpec`` or
519+
def _is_param_expr(obj):
520+
"""Checks if obj matches either a list of types, ``...``, ``ParamSpec`` or
519521
``_ConcatenateGenericAlias`` from typing.py
520522
"""
521-
if len(args) != 2:
522-
return False
523-
obj = args[0]
524523
if obj is Ellipsis:
525524
return True
525+
if isinstance(obj, list):
526+
return True
526527
obj = type(obj)
527528
names = ('ParamSpec', '_ConcatenateGenericAlias')
528529
return obj.__module__ == 'typing' and any(obj.__name__ == name for name in names)
529530

530-
531531
def _type_repr(obj):
532532
"""Return the repr() of an object, special-casing types (internal helper).
533533

Lib/test/test_typing.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,17 +581,33 @@ def test_paramspec(self):
581581
Callable = self.Callable
582582
fullname = f"{Callable.__module__}.Callable"
583583
P = ParamSpec('P')
584+
P2 = ParamSpec('P2')
584585
C1 = Callable[P, T]
585586
# substitution
586-
self.assertEqual(C1[int, str], Callable[[int], str])
587+
self.assertEqual(C1[[int], str], Callable[[int], str])
587588
self.assertEqual(C1[[int, str], str], Callable[[int, str], str])
589+
self.assertEqual(C1[[], str], Callable[[], str])
590+
self.assertEqual(C1[..., str], Callable[..., str])
591+
self.assertEqual(C1[P2, str], Callable[P2, str])
592+
self.assertEqual(C1[Concatenate[int, P2], str],
593+
Callable[Concatenate[int, P2], str])
588594
self.assertEqual(repr(C1), f"{fullname}[~P, ~T]")
589-
self.assertEqual(repr(C1[int, str]), f"{fullname}[[int], str]")
595+
self.assertEqual(repr(C1[[int, str], str]), f"{fullname}[[int, str], str]")
596+
with self.assertRaises(TypeError):
597+
C1[int, str]
590598

591599
C2 = Callable[P, int]
600+
self.assertEqual(C2[[int]], Callable[[int], int])
601+
self.assertEqual(C2[[int, str]], Callable[[int, str], int])
602+
self.assertEqual(C2[[]], Callable[[], int])
603+
self.assertEqual(C2[...], Callable[..., int])
604+
self.assertEqual(C2[P2], Callable[P2, int])
605+
self.assertEqual(C2[Concatenate[int, P2]],
606+
Callable[Concatenate[int, P2], int])
592607
# special case in PEP 612 where
593608
# X[int, str, float] == X[[int, str, float]]
594-
self.assertEqual(C2[int, str, float], C2[[int, str, float]])
609+
self.assertEqual(C2[int], Callable[[int], int])
610+
self.assertEqual(C2[int, str], Callable[[int, str], int])
595611
self.assertEqual(repr(C2), f"{fullname}[~P, int]")
596612
self.assertEqual(repr(C2[int, str]), f"{fullname}[[int, str], int]")
597613

@@ -4656,6 +4672,29 @@ class Z(Generic[P]):
46564672
self.assertEqual(G5.__parameters__, G6.__parameters__)
46574673
self.assertEqual(G5, G6)
46584674

4675+
G7 = Z[int]
4676+
self.assertEqual(G7.__args__, ((int,),))
4677+
self.assertEqual(G7.__parameters__, ())
4678+
4679+
with self.assertRaisesRegex(TypeError, "many arguments for"):
4680+
Z[[int, str], bool]
4681+
with self.assertRaisesRegex(TypeError, "many arguments for"):
4682+
Z[P_2, bool]
4683+
4684+
def test_multiple_paramspecs_in_user_generics(self):
4685+
P = ParamSpec("P")
4686+
P2 = ParamSpec("P2")
4687+
4688+
class X(Generic[P, P2]):
4689+
f: Callable[P, int]
4690+
g: Callable[P2, str]
4691+
4692+
G1 = X[[int, str], [bytes]]
4693+
G2 = X[[int], [str, bytes]]
4694+
self.assertNotEqual(G1, G2)
4695+
self.assertEqual(G1.__args__, ((int, str), (bytes,)))
4696+
self.assertEqual(G2.__args__, ((int,), (str, bytes)))
4697+
46594698
def test_no_paramspec_in__parameters__(self):
46604699
# ParamSpec should not be found in __parameters__
46614700
# of generics. Usages outside Callable, Concatenate

Lib/typing.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ def _type_check(arg, msg, is_argument=True, module=None):
182182
return arg
183183

184184

185+
def _is_param_expr(arg):
186+
return arg is ... or isinstance(arg,
187+
(tuple, list, ParamSpec, _ConcatenateGenericAlias))
188+
189+
185190
def _type_repr(obj):
186191
"""Return the repr() of an object, special-casing types (internal helper).
187192
@@ -236,7 +241,9 @@ def _prepare_paramspec_params(cls, params):
236241
variables (internal helper).
237242
"""
238243
# Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
239-
if len(cls.__parameters__) == 1 and len(params) > 1:
244+
if (len(cls.__parameters__) == 1
245+
and params and not _is_param_expr(params[0])):
246+
assert isinstance(cls.__parameters__[0], ParamSpec)
240247
return (params,)
241248
else:
242249
_check_generic(cls, params, len(cls.__parameters__))
@@ -1033,7 +1040,13 @@ def __getitem__(self, params):
10331040
new_args = []
10341041
for arg in self.__args__:
10351042
if isinstance(arg, self._typevar_types):
1036-
arg = subst[arg]
1043+
if isinstance(arg, ParamSpec):
1044+
arg = subst[arg]
1045+
if not _is_param_expr(arg):
1046+
raise TypeError(f"Expected a list of types, an ellipsis, "
1047+
f"ParamSpec, or Concatenate. Got {arg}")
1048+
else:
1049+
arg = subst[arg]
10371050
elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)):
10381051
subparams = arg.__parameters__
10391052
if subparams:
@@ -1131,17 +1144,15 @@ class _CallableGenericAlias(_GenericAlias, _root=True):
11311144
def __repr__(self):
11321145
assert self._name == 'Callable'
11331146
args = self.__args__
1134-
if len(args) == 2 and (args[0] is Ellipsis
1135-
or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
1147+
if len(args) == 2 and _is_param_expr(args[0]):
11361148
return super().__repr__()
11371149
return (f'typing.Callable'
11381150
f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
11391151
f'{_type_repr(args[-1])}]')
11401152

11411153
def __reduce__(self):
11421154
args = self.__args__
1143-
if not (len(args) == 2 and (args[0] is Ellipsis
1144-
or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
1155+
if not (len(args) == 2 and _is_param_expr(args[0])):
11451156
args = list(args[:-1]), args[-1]
11461157
return operator.getitem, (Callable, args)
11471158

@@ -1864,8 +1875,7 @@ def get_args(tp):
18641875
if isinstance(tp, (_GenericAlias, GenericAlias)):
18651876
res = tp.__args__
18661877
if (tp.__origin__ is collections.abc.Callable
1867-
and not (res[0] is Ellipsis
1868-
or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
1878+
and not (len(res) == 2 and _is_param_expr(res[0]))):
18691879
res = (list(res[:-1]), res[-1])
18701880
return res
18711881
if isinstance(tp, types.UnionType):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ensure that the :class:`~typing.ParamSpec` variable in Callable
2+
can only be substituted with a parameters expression (a list of types,
3+
an ellipsis, ParamSpec or Concatenate).

0 commit comments

Comments
 (0)