Skip to content

Commit 6dd3da3

Browse files
bpo-42195: Override _CallableGenericAlias's __getitem__ (GH-23915)
Added `__getitem__` for `_CallableGenericAlias` so that it returns a subclass (itself) of `types.GenericAlias` rather than the default behavior of returning a plain `types.GenericAlias`. This fixes `repr` issues occuring after `TypeVar` substitution arising from the previous behavior.
1 parent eee1c77 commit 6dd3da3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Lib/_collections_abc.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def __create_ga(cls, origin, args):
434434
raise TypeError(
435435
"Callable must be used as Callable[[arg, ...], result].")
436436
t_args, t_result = args
437-
if isinstance(t_args, list):
437+
if isinstance(t_args, (list, tuple)):
438438
ga_args = tuple(t_args) + (t_result,)
439439
# This relaxes what t_args can be on purpose to allow things like
440440
# PEP 612 ParamSpec. Responsibility for whether a user is using
@@ -456,6 +456,16 @@ def __reduce__(self):
456456
args = list(args[:-1]), args[-1]
457457
return _CallableGenericAlias, (Callable, args)
458458

459+
def __getitem__(self, item):
460+
# Called during TypeVar substitution, returns the custom subclass
461+
# rather than the default types.GenericAlias object.
462+
ga = super().__getitem__(item)
463+
args = ga.__args__
464+
t_result = args[-1]
465+
t_args = args[:-1]
466+
args = (t_args, t_result)
467+
return _CallableGenericAlias(Callable, args)
468+
459469

460470
def _type_repr(obj):
461471
"""Return the repr() of an object, special-casing types (internal helper).

Lib/test/test_genericalias.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ def test_abc_callable(self):
347347
self.assertEqual(C2[int, float, str], Callable[[int, float], str])
348348
self.assertEqual(C3[int], Callable[..., int])
349349

350+
# multi chaining
351+
C4 = C2[int, V, str]
352+
self.assertEqual(repr(C4).split(".")[-1], "Callable[[int, ~V], str]")
353+
self.assertEqual(repr(C4[dict]).split(".")[-1], "Callable[[int, dict], str]")
354+
self.assertEqual(C4[dict], Callable[[int, dict], str])
355+
350356
with self.subTest("Testing type erasure"):
351357
class C1(Callable):
352358
def __call__(self):

0 commit comments

Comments
 (0)