]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
authorGregory Beauregard <greg@greg.red>
Thu, 3 Mar 2022 01:14:52 +0000 (17:14 -0800)
committerGitHub <noreply@github.com>
Thu, 3 Mar 2022 01:14:52 +0000 (17:14 -0800)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2022-02-09-22-40-11.bpo-46643.aBlIx1.rst [new file with mode: 0644]

index 8fcc24c25eb95b78740ee2b333398bf30d50ccaa..bd9920436223cef3ebb5c69b3e096f6f2ad7a208 100644 (file)
@@ -5187,6 +5187,18 @@ class ParamSpecTests(BaseTestCase):
         self.assertEqual(repr(P.args), "P.args")
         self.assertEqual(repr(P.kwargs), "P.kwargs")
 
+    def test_stringized(self):
+        P = ParamSpec('P')
+        class C(Generic[P]):
+            func: Callable["P", int]
+            def foo(self, *args: "P.args", **kwargs: "P.kwargs"):
+                pass
+
+        self.assertEqual(gth(C, globals(), locals()), {"func": Callable[P, int]})
+        self.assertEqual(
+            gth(C.foo, globals(), locals()), {"args": P.args, "kwargs": P.kwargs}
+        )
+
     def test_user_generics(self):
         T = TypeVar("T")
         P = ParamSpec("P")
index 9d668b3cf4a2a4047f83f0941ccda5c6cef5aeec..6e0c68c842420b0a2d186fbab2ecd1c9e672489c 100644 (file)
@@ -181,7 +181,8 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
         return arg
     if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
         raise TypeError(f"Plain {arg} is not valid as type argument")
-    if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
+    if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec,
+                        ParamSpecArgs, ParamSpecKwargs)):
         return arg
     if not callable(arg):
         raise TypeError(f"{msg} Got {arg!r:.100}.")
diff --git a/Misc/NEWS.d/next/Library/2022-02-09-22-40-11.bpo-46643.aBlIx1.rst b/Misc/NEWS.d/next/Library/2022-02-09-22-40-11.bpo-46643.aBlIx1.rst
new file mode 100644 (file)
index 0000000..e8b4d66
--- /dev/null
@@ -0,0 +1 @@
+In :func:`typing.get_type_hints`, support evaluating stringified ``ParamSpecArgs`` and ``ParamSpecKwargs`` annotations. Patch by Gregory Beauregard.
\ No newline at end of file