diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 0cbfdaf88a..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? diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index 8ecce86cb2..933e65c6c7 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1276,6 +1276,12 @@ 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 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) + ): + 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..cc7d51b65e 100644 --- a/tests/functional/u/useless/useless_super_delegation.py +++ b/tests/functional/u/useless/useless_super_delegation.py @@ -303,3 +303,34 @@ 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) + + +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