Skip to content

Commit 257f5be

Browse files
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
(cherry picked from commit 75d2d94) Co-authored-by: Gregory Beauregard <[email protected]>
1 parent 60b561c commit 257f5be

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/test/test_typing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,6 +4865,18 @@ def test_args_kwargs(self):
48654865
self.assertEqual(repr(P.args), "P.args")
48664866
self.assertEqual(repr(P.kwargs), "P.kwargs")
48674867

4868+
def test_stringized(self):
4869+
P = ParamSpec('P')
4870+
class C(Generic[P]):
4871+
func: Callable["P", int]
4872+
def foo(self, *args: "P.args", **kwargs: "P.kwargs"):
4873+
pass
4874+
4875+
self.assertEqual(gth(C, globals(), locals()), {"func": Callable[P, int]})
4876+
self.assertEqual(
4877+
gth(C.foo, globals(), locals()), {"args": P.args, "kwargs": P.kwargs}
4878+
)
4879+
48684880
def test_user_generics(self):
48694881
T = TypeVar("T")
48704882
P = ParamSpec("P")

Lib/typing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
169169
return arg
170170
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
171171
raise TypeError(f"Plain {arg} is not valid as type argument")
172-
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
172+
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec,
173+
ParamSpecArgs, ParamSpecKwargs)):
173174
return arg
174175
if not callable(arg):
175176
raise TypeError(f"{msg} Got {arg!r:.100}.")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In :func:`typing.get_type_hints`, support evaluating stringified ``ParamSpecArgs`` and ``ParamSpecKwargs`` annotations. Patch by Gregory Beauregard.

0 commit comments

Comments
 (0)