]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 3 Mar 2022 02:26:50 +0000 (18:26 -0800)
committerGitHub <noreply@github.com>
Thu, 3 Mar 2022 02:26:50 +0000 (18:26 -0800)
(cherry picked from commit 75d2d945b4e28ca34506b2d4902367b61a8dff82)

Co-authored-by: Gregory Beauregard <greg@greg.red>
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 8ced27824205dfd1fc08827ef1bbfea437819692..f3ebece4b4a7fa4b194c4f484b26b1d0d5731d1a 100644 (file)
@@ -4865,6 +4865,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 5d900dd6c33ab62431e7a2f96b989fa8f890fa51..4519cdbc951c95cdf09a2ff6aba23730c101651d 100644 (file)
@@ -169,7 +169,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