From 6e4d89ffba19ca3237a0cbda63a19e698a879769 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Sun, 9 Apr 2023 00:16:32 +0200 Subject: [PATCH] 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 --- doc/whatsnew/fragments/8555.false_positive | 3 +++ pylint/checkers/method_args.py | 2 ++ .../p/positional_only_arguments_expected.py | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 doc/whatsnew/fragments/8555.false_positive diff --git a/doc/whatsnew/fragments/8555.false_positive b/doc/whatsnew/fragments/8555.false_positive new file mode 100644 index 0000000000..157486b399 --- /dev/null +++ b/doc/whatsnew/fragments/8555.false_positive @@ -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 diff --git a/pylint/checkers/method_args.py b/pylint/checkers/method_args.py index 37db2fa791..59083fa25a 100644 --- a/pylint/checkers/method_args.py +++ b/pylint/checkers/method_args.py @@ -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: diff --git a/tests/functional/p/positional_only_arguments_expected.py b/tests/functional/p/positional_only_arguments_expected.py index 7bde59ab84..98a2d65f5c 100644 --- a/tests/functional/p/positional_only_arguments_expected.py +++ b/tests/functional/p/positional_only_arguments_expected.py @@ -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")