From 21334cc36d0897671fc8e74a9d1aef98c4335833 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 13 Sep 2025 15:07:48 +0300 Subject: [PATCH] gh-138859: Fix `TypeError` when there is a `ParamSpec` substitution with a default --- Lib/test/test_typing.py | 22 +++++++++++++++++++ Lib/typing.py | 2 +- ...-09-13-15-07-08.gh-issue-138859.74INn1.rst | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-09-13-15-07-08.gh-issue-138859.74INn1.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 8238c62f0715f8..de12dfee8f39fa 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1172,6 +1172,28 @@ class C(Generic[*Ts]): pass eval(expected_str) ) + def test_paramspec_default_subst(self): + # See https://github.com/python/cpython/issues/138859 + + P_default = ParamSpec("P_default", default=...) + T = TypeVar("T") + T_default = TypeVar("T_default", default=object) + + class A(Generic[T, P_default, T_default]): pass + + # Must not raise: + ga = A[int] + self.assertEqual(ga.__args__, (int, ..., object)) + self.assertEqual(ga.__parameters__, ()) + + ga = A[int, [str, complex]] + self.assertEqual(ga.__args__, (int, (str, complex), object)) + self.assertEqual(ga.__parameters__, ()) + + ga = A[int, [str, complex], float] + self.assertEqual(ga.__args__, ((int, (str, complex), float))) + self.assertEqual(ga.__parameters__, ()) + class UnpackTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index babe3c44d9dc55..5b76a5ef3aa6f0 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1100,7 +1100,7 @@ def _paramspec_prepare_subst(self, alias, args): params = alias.__parameters__ i = params.index(self) if i == len(args) and self.has_default(): - args = [*args, self.__default__] + args = (*args, self.__default__) if i >= len(args): raise TypeError(f"Too few arguments for {alias}") # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612. diff --git a/Misc/NEWS.d/next/Library/2025-09-13-15-07-08.gh-issue-138859.74INn1.rst b/Misc/NEWS.d/next/Library/2025-09-13-15-07-08.gh-issue-138859.74INn1.rst new file mode 100644 index 00000000000000..6eb16ba9e11d17 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-13-15-07-08.gh-issue-138859.74INn1.rst @@ -0,0 +1 @@ +Fixes a :exc:`TypeError` when substituting ``ParamSpec`` with a default.