Pylint raises the warning: Useless super delegation in method '__init__' (useless-super-delegation)
for the SpecificError
class below.
class MyProjectExceptions(Exception):
"""The base class for all my project's exceptions."""
def __init__(self, message, func):
super(MyProjectExceptions, self).__init__(message) # Normal exception handling
self.func = func # Error origin for logging
class SpecificError(MyProjectExceptions):
"""Raise when a specific error occurs."""
def __init__(self, message, func):
super(SpecificError, self).__init__(message, func)
What is the correct way to pass the parameters to the superclass here?
If you call the SpecificError
class it will look for an __init__
. If it doesn't exist it will call the parent's __init__
. There is no need to add an __init__
here, since it's exactly the same like the parent class. It is basically code duplication.
You should do:
class SpecificError(MyProjectExceptions):
"""Raise when a specific error occurs."""
As Mathieu mentioned, you don't need to override __init__
with a call to super()
:
class SpecificError(MyProjectExceptions):
"""Raise when a specific error occurs."""
Pylint's developers explain useless-super-delegation
in a bit more depth on the Pylint changelog:
useless-super-delegation
[is] used whenever we can detect that an overridden method is useless, relying onsuper()
delegation to do the same thing as another method from the MRO.For instance, in this example, the first two methods are useless, since they do the exact same thing as the methods from the base classes, while the next two methods are not, since they do some extra operations with the passed arguments.
class Impl(Base): def __init__(self, param1, param2): super(Impl, self).__init__(param1, param2) def useless(self, first, second): return super(Impl, self).useless(first, second) def not_useless(self, first, **kwargs): debug = kwargs.pop('debug', False) if debug: ... return super(Impl, self).not_useless(first, **kwargs) def not_useless_1(self, first, *args): return super(Impl, self).not_useless_1(first + some_value, *args)
What's New In Pylint 1.7
The warning's creator further explains in a commit message:
This is used whenever pylint can detect than an overridden method is useless, relying on
super()
delegation to implement the same thing as another method from MRO. In this case, it is enough to not implement the given method and let it be propagated to the another implementation from the MRO.
For the original issue report, see #839 - Pylint rule against people overriding a method just to call super.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With