]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42195: Override _CallableGenericAlias's __getitem__ (GH-23915)
authorkj <28750310+Fidget-Spinner@users.noreply.github.com>
Thu, 24 Dec 2020 02:47:40 +0000 (10:47 +0800)
committerGitHub <noreply@github.com>
Thu, 24 Dec 2020 02:47:40 +0000 (18:47 -0800)
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.

Lib/_collections_abc.py
Lib/test/test_genericalias.py

index 7c3faa64ea7f981d93742f536de077311c587047..e4eac7964623596f9871b168a78f42e489adb0e6 100644 (file)
@@ -434,7 +434,7 @@ class _CallableGenericAlias(GenericAlias):
             raise TypeError(
                 "Callable must be used as Callable[[arg, ...], result].")
         t_args, t_result = args
-        if isinstance(t_args, list):
+        if isinstance(t_args, (list, tuple)):
             ga_args = tuple(t_args) + (t_result,)
         # This relaxes what t_args can be on purpose to allow things like
         # PEP 612 ParamSpec.  Responsibility for whether a user is using
@@ -456,6 +456,16 @@ class _CallableGenericAlias(GenericAlias):
             args = list(args[:-1]), args[-1]
         return _CallableGenericAlias, (Callable, args)
 
+    def __getitem__(self, item):
+        # Called during TypeVar substitution, returns the custom subclass
+        # rather than the default types.GenericAlias object.
+        ga = super().__getitem__(item)
+        args = ga.__args__
+        t_result = args[-1]
+        t_args = args[:-1]
+        args = (t_args, t_result)
+        return _CallableGenericAlias(Callable, args)
+
 
 def _type_repr(obj):
     """Return the repr() of an object, special-casing types (internal helper).
index 5de13fe6d2f68c54624fbec0207c7d306ae3e39a..ccf40b13d3a94c00eaf4c87cf5cbcce94646d7a0 100644 (file)
@@ -347,6 +347,12 @@ class BaseTest(unittest.TestCase):
             self.assertEqual(C2[int, float, str], Callable[[int, float], str])
             self.assertEqual(C3[int], Callable[..., int])
 
+            # multi chaining
+            C4 = C2[int, V, str]
+            self.assertEqual(repr(C4).split(".")[-1], "Callable[[int, ~V], str]")
+            self.assertEqual(repr(C4[dict]).split(".")[-1], "Callable[[int, dict], str]")
+            self.assertEqual(C4[dict], Callable[[int, dict], str])
+
         with self.subTest("Testing type erasure"):
             class C1(Callable):
                 def __call__(self):