Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102615: Use list instead of tuple in repr of paramspec #102637

Merged
merged 5 commits into from Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_typing.py
Expand Up @@ -3809,6 +3809,23 @@ class Y(C[int]):
self.assertEqual(Y.__qualname__,
'GenericTests.test_repr_2.<locals>.Y')

def test_repr_3(self):
T = TypeVar('T')
P = ParamSpec('P')

class MyCallable(Generic[P, T]):
pass

object_to_expected_repr = {
MyCallable[[], bool]: "MyCallable[[], bool]",
MyCallable[[int], bool]: "MyCallable[[int], bool]",
MyCallable[[int, str], bool]: "MyCallable[[int, str], bool]"
}
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

for obj, expected_repr in object_to_expected_repr.items():
with self.subTest(obj=obj, expected_repr=expected_repr):
self.assertEqual(repr(obj), MyCallable.__module__ + expected_repr)
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

def test_eq_1(self):
self.assertEqual(Generic, Generic)
self.assertEqual(Generic[T], Generic[T])
Expand Down
5 changes: 4 additions & 1 deletion Lib/typing.py
Expand Up @@ -237,9 +237,12 @@ def _type_repr(obj):
return obj.__qualname__
return f'{obj.__module__}.{obj.__qualname__}'
if obj is ...:
return('...')
return '...'
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(obj, types.FunctionType):
return obj.__name__
if isinstance(obj, tuple):
# Special case for `repr` of types with `ParamSpec`:
return '[' + ', '.join(_type_repr(t) for t in obj) + ']'
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
return repr(obj)


Expand Down
@@ -0,0 +1,3 @@
Typing: Improve the ``repr`` of generic aliases for classes generic over a
:class:`~typing.ParamSpec`. (Use square brackets to represent a parameter
list.)