Skip to content

Commit 75d2d94

Browse files
authored
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
1 parent 50ec345 commit 75d2d94

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
@@ -5187,6 +5187,18 @@ def test_args_kwargs(self):
51875187
self.assertEqual(repr(P.args), "P.args")
51885188
self.assertEqual(repr(P.kwargs), "P.kwargs")
51895189

5190+
def test_stringized(self):
5191+
P = ParamSpec('P')
5192+
class C(Generic[P]):
5193+
func: Callable["P", int]
5194+
def foo(self, *args: "P.args", **kwargs: "P.kwargs"):
5195+
pass
5196+
5197+
self.assertEqual(gth(C, globals(), locals()), {"func": Callable[P, int]})
5198+
self.assertEqual(
5199+
gth(C.foo, globals(), locals()), {"args": P.args, "kwargs": P.kwargs}
5200+
)
5201+
51905202
def test_user_generics(self):
51915203
T = TypeVar("T")
51925204
P = ParamSpec("P")

Lib/typing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
181181
return arg
182182
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
183183
raise TypeError(f"Plain {arg} is not valid as type argument")
184-
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
184+
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec,
185+
ParamSpecArgs, ParamSpecKwargs)):
185186
return arg
186187
if not callable(arg):
187188
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)