mirror of
https://github.com/python/cpython.git
synced 2026-01-07 16:02:55 +00:00
bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238)
This commit is contained in:
parent
50ec3453c5
commit
75d2d945b4
3 changed files with 15 additions and 1 deletions
|
|
@ -5187,6 +5187,18 @@ def test_args_kwargs(self):
|
|||
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")
|
||||
|
|
|
|||
|
|
@ -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}.")
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
In :func:`typing.get_type_hints`, support evaluating stringified ``ParamSpecArgs`` and ``ParamSpecKwargs`` annotations. Patch by Gregory Beauregard.
|
||||
Loading…
Add table
Add a link
Reference in a new issue