Skip to content

Commit

Permalink
Fix false positive for useless-super-delegation for variadics (#6949
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DanielNoord authored and Pierre-Sassoulas committed Jun 18, 2022
1 parent f881219 commit b9ecb4d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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?
----------------------------
Expand Down
6 changes: 6 additions & 0 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions tests/functional/u/useless/useless_super_delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/functional/u/useless/useless_super_delegation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b9ecb4d

Please sign in to comment.