Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes a :exc:`TypeError` when substituting ``ParamSpec`` with a default.
Loading