From e03ea4a2aea88976f729e804fa54cff64c36c7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Tue, 14 Jun 2022 15:12:25 +0200 Subject: [PATCH 1/6] Fix false positive for ``useless-super-delegation`` for variadics --- doc/whatsnew/2/2.14/full.rst | 5 +++++ pylint/checkers/classes/class_checker.py | 8 ++++++++ tests/functional/u/useless/useless_super_delegation.py | 10 ++++++++++ 3 files changed, 23 insertions(+) diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 00cf0528e1..ae1cbea452 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -27,6 +27,11 @@ Release date: TBA Closes #6818 +* Fixed a false positive for ``useless-super-delegation`` for subclasses that specify the number of + of parameters against a parent that uses a variadic argument. + + Closes #2270 + * Fixed an issue with multi-line ``init-hook`` options which did not record the line endings. Closes #6888 diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index 8ecce86cb2..acdb01a548 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1276,6 +1276,14 @@ def _check_useless_super_delegation(self, function: nodes.FunctionDef) -> None: args = _signature_from_call(call) if meth_node is not None: + # Detect if the super method uses varargs or kwargs and the function doesn't + if ( + meth_node.args.vararg + and not function.args.vararg + or meth_node.args.kwarg + and not function.args.kwarg + ): + return def form_annotations(arguments): annotations = chain( diff --git a/tests/functional/u/useless/useless_super_delegation.py b/tests/functional/u/useless/useless_super_delegation.py index 2a7ecb787c..5bc9c34862 100644 --- a/tests/functional/u/useless/useless_super_delegation.py +++ b/tests/functional/u/useless/useless_super_delegation.py @@ -303,3 +303,13 @@ def __str__(self): def __hash__(self): # [useless-super-delegation] return super().__hash__() + + +# Reported in https://github.com/PyCQA/pylint/issues/2270 +class Super: + def __init__(self, *args): + self.args = args + +class Sub(Super): + def __init__(self, a, b): + super().__init__(a, b) From 70177c3d30a4d92241524dcbdf1d344794617a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Tue, 14 Jun 2022 15:21:35 +0200 Subject: [PATCH 2/6] Update pylint/checkers/classes/class_checker.py --- pylint/checkers/classes/class_checker.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index acdb01a548..a649a7f773 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1276,12 +1276,10 @@ def _check_useless_super_delegation(self, function: nodes.FunctionDef) -> None: args = _signature_from_call(call) if meth_node is not None: - # Detect if the super method uses varargs or kwargs and the function doesn't + # Detect if the super method uses varargs and the function doesn't if ( meth_node.args.vararg and not function.args.vararg - or meth_node.args.kwarg - and not function.args.kwarg ): return From 94ed776357f960b1308ae5b9a0afb4f38510101d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Jun 2022 13:23:54 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint/checkers/classes/class_checker.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index a649a7f773..cc02ba113e 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1277,10 +1277,7 @@ def _check_useless_super_delegation(self, function: nodes.FunctionDef) -> None: if meth_node is not None: # Detect if the super method uses varargs and the function doesn't - if ( - meth_node.args.vararg - and not function.args.vararg - ): + if meth_node.args.vararg and not function.args.vararg: return def form_annotations(arguments): From 271666b6617ad5542b2ca1bc8a43ba168fd80500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 15 Jun 2022 09:33:41 +0200 Subject: [PATCH 4/6] Update --- pylint/checkers/classes/class_checker.py | 5 ++++- .../u/useless/useless_super_delegation.py | 21 +++++++++++++++++++ .../u/useless/useless_super_delegation.txt | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index cc02ba113e..d168a85771 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1277,7 +1277,10 @@ def _check_useless_super_delegation(self, function: nodes.FunctionDef) -> None: if meth_node is not None: # Detect if the super method uses varargs and the function doesn't - if meth_node.args.vararg and not function.args.vararg: + if meth_node.args.vararg and ( + not function.args.vararg + or len(function.args.args) > len(meth_node.args.args) + ): return def form_annotations(arguments): diff --git a/tests/functional/u/useless/useless_super_delegation.py b/tests/functional/u/useless/useless_super_delegation.py index 5bc9c34862..cc7d51b65e 100644 --- a/tests/functional/u/useless/useless_super_delegation.py +++ b/tests/functional/u/useless/useless_super_delegation.py @@ -310,6 +310,27 @@ class Super: def __init__(self, *args): self.args = args + class Sub(Super): def __init__(self, a, b): super().__init__(a, b) + + +class SubTwo(Super): + def __init__(self, a, *args): + super().__init__(a, *args) + + +class SuperTwo: + def __init__(self, a, *args): + self.args = args + + +class SubTwoOne(SuperTwo): + def __init__(self, a, *args): # [useless-super-delegation] + super().__init__(a, *args) + + +class SubTwoTwo(SuperTwo): + def __init__(self, a, b, *args): + super().__init__(a, b, *args) diff --git a/tests/functional/u/useless/useless_super_delegation.txt b/tests/functional/u/useless/useless_super_delegation.txt index 8184524afa..9cf3115732 100644 --- a/tests/functional/u/useless/useless_super_delegation.txt +++ b/tests/functional/u/useless/useless_super_delegation.txt @@ -18,3 +18,4 @@ useless-super-delegation:264:4:264:28:UselessSuper.with_default_arg_bis:Useless useless-super-delegation:267:4:267:28:UselessSuper.with_default_arg_ter:Useless super delegation in method 'with_default_arg_ter':UNDEFINED useless-super-delegation:270:4:270:29:UselessSuper.with_default_arg_quad:Useless super delegation in method 'with_default_arg_quad':UNDEFINED useless-super-delegation:304:4:304:16:DecoratedList.__hash__:Useless super delegation in method '__hash__':UNDEFINED +useless-super-delegation:330:4:330:16:SubTwoOne.__init__:Useless super delegation in method '__init__':UNDEFINED From d70ea497532dae76ee36366662b433a39ad582a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 15 Jun 2022 12:17:34 +0200 Subject: [PATCH 5/6] Update pylint/checkers/classes/class_checker.py --- pylint/checkers/classes/class_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index d168a85771..933e65c6c7 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1276,7 +1276,7 @@ def _check_useless_super_delegation(self, function: nodes.FunctionDef) -> None: args = _signature_from_call(call) if meth_node is not None: - # Detect if the super method uses varargs and the function doesn't + # Detect if the super method uses varargs and the function doesn't or makes some of those explicit if meth_node.args.vararg and ( not function.args.vararg or len(function.args.args) > len(meth_node.args.args) From 90ec8d413e86072febcc5c922607b246e5809e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 15 Jun 2022 13:35:28 +0200 Subject: [PATCH 6/6] Move changelog --- doc/whatsnew/2/2.14/full.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 81bf493684..885be244e4 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -5,7 +5,10 @@ What's New in Pylint 2.14.3? ---------------------------- Release date: TBA +* Fixed a false positive for ``useless-super-delegation`` for subclasses that specify the number of + of parameters against a parent that uses a variadic argument. + Closes #2270 What's New in Pylint 2.14.2? @@ -33,11 +36,6 @@ Release date: 2022-06-15 Closes #6818 -* Fixed a false positive for ``useless-super-delegation`` for subclasses that specify the number of - of parameters against a parent that uses a variadic argument. - - Closes #2270 - * Fixed an issue with multi-line ``init-hook`` options which did not record the line endings. Closes #6888