Skip to content

Commit 21334cc

Browse files
committed
gh-138859: Fix TypeError when there is a ParamSpec substitution with a default
1 parent 805e336 commit 21334cc

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/test/test_typing.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,28 @@ class C(Generic[*Ts]): pass
11721172
eval(expected_str)
11731173
)
11741174

1175+
def test_paramspec_default_subst(self):
1176+
# See https://github.com/python/cpython/issues/138859
1177+
1178+
P_default = ParamSpec("P_default", default=...)
1179+
T = TypeVar("T")
1180+
T_default = TypeVar("T_default", default=object)
1181+
1182+
class A(Generic[T, P_default, T_default]): pass
1183+
1184+
# Must not raise:
1185+
ga = A[int]
1186+
self.assertEqual(ga.__args__, (int, ..., object))
1187+
self.assertEqual(ga.__parameters__, ())
1188+
1189+
ga = A[int, [str, complex]]
1190+
self.assertEqual(ga.__args__, (int, (str, complex), object))
1191+
self.assertEqual(ga.__parameters__, ())
1192+
1193+
ga = A[int, [str, complex], float]
1194+
self.assertEqual(ga.__args__, ((int, (str, complex), float)))
1195+
self.assertEqual(ga.__parameters__, ())
1196+
11751197

11761198
class UnpackTests(BaseTestCase):
11771199

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ def _paramspec_prepare_subst(self, alias, args):
11001100
params = alias.__parameters__
11011101
i = params.index(self)
11021102
if i == len(args) and self.has_default():
1103-
args = [*args, self.__default__]
1103+
args = (*args, self.__default__)
11041104
if i >= len(args):
11051105
raise TypeError(f"Too few arguments for {alias}")
11061106
# Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes a :exc:`TypeError` when substituting ``ParamSpec`` with a default.

0 commit comments

Comments
 (0)