-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Lower severity message than redundant-keyword-arg
for calling distinct positional-only arg and variadic keyword arg with same name
#8558
Comments
The idea for this checker also crossed my mind while pondering #8555 but I dismissed the idea as noise if it is expected Python behaviour. But now that you are also having the same thought then I think there must be some value to it! |
I'm on mobile so I can't link to the proper documentation but it seems close to what 'keyword arg before vararg' (w1113) does. |
Interestingly, if you change the def foo(name="Sarah", /, *args):
print(name)
foo(name="Jacob")
Yes, we could consider raising that very message, except that the argument we're talking about is supposedly not a keyword arg. |
Even more interestingly, even >>> import inspect
>>> def foo(name="Sarah", /, **kwds):
... return name
...
>>> inspect.signature(foo).bind(name="Jacob")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 3211, in bind
return self._bind(args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/inspect.py", line 3107, in _bind
raise TypeError(msg) from None
TypeError: 'name' parameter is positional only, but was passed as a keyword
>>> foo(name="Jacob")
'Sarah' |
Thanks for looking into it @jacobtylerwalls; please feel free to go ahead with opening an issue since you had the insight and idea to do so. Also I'm tired as heck today :D |
I'm changing my mind now; I see there's no issue in CPython. Mark's comments on #8556 were helpful, as was python/cpython#87106. I want to suggest instead warning on the problematic call, not on the function definition itself. |
Okay, finally figured out what to suggest here. #8556 makes this situation better; we should merge it. Then, we still have two independent problems: 1 # pylint: disable=missing-function-docstring, missing-module-docstring
2 def required_positional_with_var_kwargs(a, /, **_kwargs):
3 return a
4
5 def optional_positional_with_var_kwargs(a=1, /, **_kwargs):
6 return a
7
8
9 # 3 of these 4 succeed, but it's a bit smelly
10 required_positional_with_var_kwargs(42, a=43) # returns 42
11 required_positional_with_var_kwargs(a=43) # TypeError: should raise no-value-for-parameter
12
13 optional_positional_with_var_kwargs(42, a=43) # returns 42
14 optional_positional_with_var_kwargs(a=43) # returns 1 Currently only gives:
1 2 We should raise This card can be for the first suggestion. Retitling. |
redundant-keyword-arg
for calling distinct positional-only arg and variadic keyword arg with same name
Enh, or we could just not emit any message at all for 1. One way or another, it's a bug, not an enhancement. |
…ith a keyword-only-parameter and ``**kwargs``, is called with a positional argument and a keyword argument where the keyword argument has the same name as the positional-only-parameter. Closes pylint-dev#8558
…ith a keyword-only-parameter and ``**kwargs``, is called with a positional argument and a keyword argument where the keyword argument has the same name as the positional-only-parameter. Closes pylint-dev#8558
…with a keyword argument which shares a name with a positional-only parameter and the function contains a keyword variadic parameter dictionary. It may be surprising behaviour when the keyword argument is added to the keyword variadic parameter dictionary. Closes pylint-dev#8558
…positive (#8644) * Fix a false positive for ``redundant-keyword-arg`` when a function, with a keyword-only-parameter and ``**kwargs``, is called with a positional argument and a keyword argument where the keyword argument has the same name as the positional-only-parameter. * Add new checker ``kwarg-superseded-by-positional-arg`` which emits a warning message for the above scenario. Closes #8558 Co-authored-by: Pierre Sassoulas <[email protected]>
Current problem
When writing a function, you can defeat the point of a PEP 570 positional-only argument (to the left of
/
) if:**kwargs
GOOD example (adapted from PEP 570, "Semantic Corner Case"):
See that name is actually positional-only:
BAD example:
See that name is completely ignored:
Desired solution
I suggest a new warning e.g.
defeated-positional-only-argument
Additional context
Inspired by #8555
PEP 570
The text was updated successfully, but these errors were encountered: