Skip to content

Commit 8e4620c

Browse files
committed
pythongh-102615: Use list instead of tuple in repr of paramspec
1 parent bb396ee commit 8e4620c

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎Lib/test/test_typing.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,26 @@ class Y(C[int]):
38093809
self.assertEqual(Y.__qualname__,
38103810
'GenericTests.test_repr_2.<locals>.Y')
38113811

3812+
def test_repr_3(self):
3813+
T = TypeVar('T')
3814+
P = ParamSpec('P')
3815+
3816+
class MyCallable(Generic[P, T]):
3817+
pass
3818+
3819+
self.assertEqual(
3820+
repr(MyCallable[[], bool]).split('.')[-1],
3821+
'MyCallable[[], bool]',
3822+
)
3823+
self.assertEqual(
3824+
repr(MyCallable[[int], bool]).split('.')[-1],
3825+
'MyCallable[[int], bool]',
3826+
)
3827+
self.assertEqual(
3828+
repr(MyCallable[[int, str], bool]).split('.')[-1],
3829+
"MyCallable[[int, str], bool]",
3830+
)
3831+
38123832
def test_eq_1(self):
38133833
self.assertEqual(Generic, Generic)
38143834
self.assertEqual(Generic[T], Generic[T])

‎Lib/typing.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,12 @@ def _type_repr(obj):
237237
return obj.__qualname__
238238
return f'{obj.__module__}.{obj.__qualname__}'
239239
if obj is ...:
240-
return('...')
240+
return '...'
241241
if isinstance(obj, types.FunctionType):
242242
return obj.__name__
243+
if isinstance(obj, tuple):
244+
# Special case for `repr` of types with `ParamSpec`:
245+
return '[' + ', '.join(_type_repr(t) for t in obj) + ']'
243246
return repr(obj)
244247

245248

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use list instead of tuple in ``repr`` of generic aliases with ``ParamSpec``.

0 commit comments

Comments
 (0)