🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Address review
  • Loading branch information
sobolevn committed Mar 15, 2023
commit 3451d4d6de7034e63d792eac8983ceeb91c41157
33 changes: 29 additions & 4 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3811,22 +3811,47 @@ class Y(C[int]):

def test_repr_3(self):
T = TypeVar('T')
T1 = TypeVar('T1')
P = ParamSpec('P')
P2 = ParamSpec('P2')
Ts = TypeVarTuple('Ts')

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

class DoubleSpec(Generic[P, P2, T]):
pass

class TsP(Generic[*Ts, P]):
pass

object_to_expected_repr = {
MyCallable[[], bool]: "MyCallable[[], bool]",
MyCallable[[int], bool]: "MyCallable[[int], bool]",
MyCallable[[int, str], bool]: "MyCallable[[int, str], bool]"
MyCallable[P, T]: "MyCallable[~P, ~T]",
MyCallable[Concatenate[T1, P], T]: "MyCallable[typing.Concatenate[~T1, ~P], ~T]",
MyCallable[[], bool]: "MyCallable[[], bool]",
MyCallable[[int], bool]: "MyCallable[[int], bool]",
MyCallable[[int, str], bool]: "MyCallable[[int, str], bool]",
MyCallable[[int, list[int]], bool]: "MyCallable[[int, list[int]], bool]",
MyCallable[Concatenate[*Ts, P], T]: "MyCallable[typing.Concatenate[*Ts, ~P], ~T]",

DoubleSpec[P2, P, T]: "DoubleSpec[~P2, ~P, ~T]",
DoubleSpec[[int], [str], bool]: "DoubleSpec[[int], [str], bool]",
DoubleSpec[[int, int], [str, str], bool]: "DoubleSpec[[int, int], [str, str], bool]",

TsP[*Ts, P]: "TsP[*Ts, ~P]",
TsP[int, str, list[int], []]: "TsP[int, str, list[int], []]",
TsP[int, [str, list[int]]]: "TsP[int, [str, list[int]]]",

# These lines are just too long to fit:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

MyCallable[Concatenate[*Ts, P], int][int, str, [bool, float]]:
"MyCallable[[int, str, bool, float], int]",
}

for obj, expected_repr in object_to_expected_repr.items():
with self.subTest(obj=obj, expected_repr=expected_repr):
self.assertRegex(
repr(obj),
fr"^{re.escape(MyCallable.__module__)}.*\.{re.escape(expected_repr)}$"
fr"^{re.escape(MyCallable.__module__)}.*\.{re.escape(expected_repr)}$",
)

def test_eq_1(self):
Expand Down
2 changes: 0 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ def _type_repr(obj):
typically enough to uniquely identify a type. For everything
else, we fall back on repr(obj).
"""
if isinstance(obj, types.GenericAlias):
return repr(obj)
if isinstance(obj, type):
if obj.__module__ == 'builtins':
return obj.__qualname__
Expand Down