Skip to content
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

[Backport maintenance/2.17.x] Fix false positive for positional-only-arguments-expected #8560

Merged
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
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8555.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix false positive for ``positional-only-arguments-expected`` when a function contains both a positional-only parameter that has a default value, and ``**kwargs``.

Closes #8555
2 changes: 2 additions & 0 deletions pylint/checkers/method_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def _check_positional_only_arguments_expected(self, node: nodes.Call) -> None:
and inferred_func.args.posonlyargs
):
return
if inferred_func.args.kwarg:
return
pos_args = [a.name for a in inferred_func.args.posonlyargs]
kws = [k.arg for k in node.keywords if k.arg in pos_args]
if not kws:
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/p/positional_only_arguments_expected.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ def nihon(self, a, r, i, /, cheese=False):
cake.nihon(1, r=2, i=3) # [positional-only-arguments-expected]
cake.nihon(a=1, r=2, i=3) # [positional-only-arguments-expected]
cake.nihon(1, r=2, i=3, cheese=True) # [positional-only-arguments-expected]


def function_with_kwargs(apple, banana="Yellow banana", /, **kwargs):
"""
Calling this function with the `banana` keyword should not emit
`positional-only-arguments-expected` since it is added to `**kwargs`.

>>> function_with_kwargs("Red apple", banana="Green banana")
>>> "Red apple"
>>> "Yellow banana"
>>> {"banana": "Green banana"}
"""
print(apple)
print(banana)
print(kwargs)


function_with_kwargs("Red apple", banana="Green banana")