From 61dae1e5f4fa4b8f2f93d8846e66f4eee6cc877f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 10 Apr 2023 05:55:35 +0000 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``. (#8556) (#8560) (cherry picked from commit db17860fd61154c59f3acc13ff34b120cc5ba091) Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> --- 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 e882482190..8862328e39 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")