Skip to content

Commit 588d421

Browse files
fix typing tests, add hash and eq methods
1 parent 2c4a297 commit 588d421

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

Lib/_collections_abc.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ def __new__(cls, *args, **kwargs):
421421
raise TypeError("Callable must be used as "
422422
"Callable[[arg, ...], result].")
423423
_typ, _args = args
424-
if not isinstance(_args, tuple) or len(_args) != 2:
424+
if not isinstance(_args, (tuple, type(...))) or len(_args) != 2:
425425
raise TypeError("Callable must be used as "
426426
"Callable[[arg, ...], result].")
427427
t_args, t_result = _args
428-
if not isinstance(t_args, list):
428+
if not isinstance(t_args, (list, type(...))):
429429
raise TypeError("Callable must be used as "
430430
"Callable[[arg, ...], result].")
431431

@@ -440,12 +440,27 @@ def __new__(cls, *args, **kwargs):
440440
def __init__(self, *args, **kwargs):
441441
pass
442442

443+
def __eq__(self, other):
444+
if not isinstance(other, GenericAlias):
445+
return NotImplemented
446+
return (self.__origin__ == other.__origin__
447+
and self.__args__ == other.__args__)
448+
449+
def __hash__(self):
450+
return super().__hash__()
451+
443452
def __repr__(self):
444453
t_args = self.__args__[:-1]
445454
t_result = self.__args__[-1]
446-
return f"{_type_repr(self.__origin__)}" \
447-
f"[[{', '.join(_type_repr(a) for a in t_args)}], " \
448-
f"{_type_repr(t_result)}]"
455+
456+
orig = _type_repr(self.__origin__)
457+
args = f"{', '.join(_type_repr(a) for a in t_args)}"
458+
result = _type_repr(t_result)
459+
460+
if not isinstance(t_args[0], type(...)):
461+
args = f"[{args}]"
462+
463+
return f"{orig}[{args}, {result}]"
449464

450465

451466
def _type_repr(obj):

Lib/test/test_typing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ def __call__(self):
18181818
## with self.assertRaises(TypeError):
18191819
## T2[int, str]
18201820

1821-
self.assertEqual(repr(C1[int]).split('.')[-1], 'C1[int]')
1821+
self.assertEqual(repr(C1[[int], int]).split('.')[-1], 'C1[[int], int]')
18221822
self.assertEqual(C2.__parameters__, ())
18231823
self.assertIsInstance(C2(), collections.abc.Callable)
18241824
self.assertIsSubclass(C2, collections.abc.Callable)
@@ -1858,8 +1858,8 @@ class MyTup(Tuple[T, T]): ...
18581858
self.assertEqual(MyTup[int]().__orig_class__, MyTup[int])
18591859
class MyCall(Callable[..., T]):
18601860
def __call__(self): return None
1861-
self.assertIs(MyCall[T]().__class__, MyCall)
1862-
self.assertEqual(MyCall[T]().__orig_class__, MyCall[T])
1861+
self.assertIs(MyCall[[T], T]().__class__, MyCall)
1862+
self.assertEqual(MyCall[[T], T]().__orig_class__, MyCall[[T], T])
18631863
class MyDict(typing.Dict[T, T]): ...
18641864
self.assertIs(MyDict[int]().__class__, MyDict)
18651865
self.assertEqual(MyDict[int]().__orig_class__, MyDict[int])

0 commit comments

Comments
 (0)