Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.10] bpo-46643: Fix stringized P.args/P.kwargs with get_type_hints (GH-31238) #31646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4865,6 +4865,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")
Expand Down
3 changes: 2 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In :func:`typing.get_type_hints`, support evaluating stringified ``ParamSpecArgs`` and ``ParamSpecKwargs`` annotations. Patch by Gregory Beauregard.