Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint warning for "useless super delegation"

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?

like image 642
mugwump Avatar asked Aug 09 '17 22:08

mugwump


2 Answers

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."""
like image 77
Xalio08 Avatar answered Oct 31 '22 16:10

Xalio08


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 on super() 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.

like image 41
Stevoisiak Avatar answered Oct 31 '22 17:10

Stevoisiak