-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Make sure ParamSpec
suffix and arg kind do match
#13468
Conversation
*args: P.kwargs, # E: Use ".args" for "*" argument | ||
**kwargs: P.args, # E: Use ".kwargs" for "**" argument | ||
) -> str: | ||
return "foo" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add some reveal_type
s here, as a sanity check to make sure that the typo doesn't cause mypy to make any incorrect inferences elsewhere?
return "foo" | |
reveal_type(args) # N: Revealed type is "builtins.tuple[builtins.object, ...]" | |
reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]" | |
return "foo" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not true:
main:11: note: Revealed type is "P1.args`-1" (diff)
main:12: note: Revealed type is "P1.kwargs`-1" (diff)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not true:
main:11: note: Revealed type is "P1.args`-1" (diff) main:12: note: Revealed type is "P1.kwargs`-1" (diff)
Oh, right. Maybe this, in that case?
return "foo" | |
a = args[0] | |
reveal_type(a) # N: Revealed type is "builtins.object" | |
for b, c in kwargs.items(): | |
reveal_type(b) # N: Revealed type is "builtins.str" | |
reveal_type(c) # N: Revealed type is "builtins.object" | |
return "foo" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is too much :)
No other tests do anything similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe:
mypy/typeanal.py
Outdated
@@ -847,8 +846,12 @@ def anal_star_arg_type(self, t: Type, kind: ArgKind, nested: bool) -> Type: | |||
if isinstance(tvar_def, ParamSpecType): | |||
if kind == ARG_STAR: | |||
make_paramspec = paramspec_args | |||
if components[-1] == "kwargs": | |||
self.fail('Use ".args" for "*" argument', t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.fail('Use ".args" for "*" argument', t) | |
self.fail(f'Use "{'.'.join(components[:-1])}.args" for variadic "*" parameter', t) |
mypy/typeanal.py
Outdated
elif kind == ARG_STAR2: | ||
make_paramspec = paramspec_kwargs | ||
if components[-1] == "args": | ||
self.fail('Use ".kwargs" for "**" argument', t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.fail('Use ".kwargs" for "**" argument', t) | |
self.fail(f'Use "{'.'.join(components[:-1])}.kwargs" for variadic "**" parameter', t) |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This comment has been minimized.
This comment has been minimized.
I will leave this open for a couple of days, maybe some other ideas about the error message will araise :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, one nit.
Another weirdness I noticed: if you do def inner(*args: P.__bound__, **kwargs: P.kwargs):
, you get main.py:6: error: Name "P.__bound__" is not defined
(expected though the wording could be improved) but also main.py:6: error: Name "P.kwargs" is not defined
(not expected). https://mypy-play.net/?mypy=latest&python=3.10&gist=17f2477b6410e476d73dc720b43b31b2
Thanks! This is something else. I will take a look later. Good catch about |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
I am mostly worried about the error message here :)
Closes #13460