Pylint: useless-super-delegation when a parameter uses a default value

Created on 24 Aug 2016  路  10Comments  路  Source: PyCQA/pylint

Given the following code, pylint will happily emit an useless-super-delegation message. I think it does not make sense that much though, since we can argue that the overridden method is not useless, since it introduces a parameter with a default value.

def __init__(self, message='...', **kws):
    super().__init__(message, **kws)
bug contributor friendly

Most helpful comment

There might be a regression in pylint-2.0.
With the following code:

ERROR_HANDLERS = (
    (FieldValueError, data.FieldValueError.from_error),
    (FieldNotNullable, data.FieldNotNullable.from_error),
    (MissingKeyError, data.MissingKeyError.from_error),
    (InvalidKeys, data.InvalidKeys.from_error),
    (InvalidEnumerationValue, data.InvalidEnumerationValue.from_error))


class Application(_Application):
    """Extends wsgilib's application."""

    def __init__(self, *args, cors=False, debug=False, errorhandlers=(),
                 **kwargs):
        """Sets default error handlers."""
        super().__init__(
            *args, cors=cors, debug=debug,
            errorhandlers=ERROR_HANDLERS + errorhandlers, **kwargs)

I get W0235: Useless super delegation in method '__init__' (useless-super-delegation).
But if I refactor it to:

ERROR_HANDLERS = (
    (FieldValueError, data.FieldValueError.from_error),
    (FieldNotNullable, data.FieldNotNullable.from_error),
    (MissingKeyError, data.MissingKeyError.from_error),
    (InvalidKeys, data.InvalidKeys.from_error),
    (InvalidEnumerationValue, data.InvalidEnumerationValue.from_error))


class Application(_Application):
    """Extends wsgilib's application."""

    def __init__(self, *args, cors=False, debug=False, errorhandlers=(),
                 **kwargs):
        """Sets default error handlers."""
        errorhandlers = ERROR_HANDLERS + errorhandlers
        super().__init__(
            *args, cors=cors, debug=debug,
            errorhandlers=errorhandlers, **kwargs)

I do not get any warning.

All 10 comments

in pylint 1.7.1 w/ python 2.7.13, it appears to also have an issue when all the args are pass through:

class NewObject(SuperObject):
    def __init__(self, name, var):
        super(NewObject, self).__init__(name, var)
    def thing(self):
        pass

That's an example of why this check exist, you don't have to call the super method if you are not doing anything in your overridden method.

^^ post updated: I made an error. I am trying to explicitly trying to pass my vars to the parent init.

@CMeza99 once again, how does your code snippet differs from doing:

class NewObject(SuperObject):
    pass

?

better? I give up.

[鈥 the overridden method is not useless, since it introduces a parameter with a default value.

There is also (as described in issue #1547) a false positive when a parameter already has a default, and the override method changes the default.

# lorem.py

class LoremIpsum:

    def odio(self, elit="consecteur"):
        print(elit)


class DolorSitAmet(LoremIpsum):

    def odio(self, elit="adipiscing"):
        super().odio(elit)

The check should not match when an override method sets or changes a default for a parameter that is different from the defaults in the superclass method.

For any beginner contributors looking at this: this is almost identical to #1553 so fixing one may fix the other. But this includes an extra case to consider where we may also want to check whether the default values match, and not just the names of the arguments.

Should this be fixed in version 1.8.2? The following code (abbreviated from https://github.com/home-assistant/home-assistant/blob/2ae0c5653ed32c8b7309686d9b897f7d7c6fc0fb/homeassistant/components/cloud/auth_api.py) still produces this error with Python 3.6.4

class PasswordChangeRequired(Exception):
    """Raised when a password change is required."""

    def __init__(self, message='Password change required.'):
        """Initialize a password change required error."""
        super().__init__(message)
$ pylint a.py --disable=all --enable=useless-super-delegation
************* Module a
W:  4, 4: Useless super delegation in method '__init__' (useless-super-delegation)

I'm seeing this with 1.8.4 for the following code:

class ExportedUserRecord(UserRecord):
    def __init__(self, data):
        super(ExportedUserRecord, self).__init__(data)

    @property
    def password_hash(self):
        return self._data.get('passwordHash')

self._data is defined in the parent class, so I have to call the parent constructor.

There might be a regression in pylint-2.0.
With the following code:

ERROR_HANDLERS = (
    (FieldValueError, data.FieldValueError.from_error),
    (FieldNotNullable, data.FieldNotNullable.from_error),
    (MissingKeyError, data.MissingKeyError.from_error),
    (InvalidKeys, data.InvalidKeys.from_error),
    (InvalidEnumerationValue, data.InvalidEnumerationValue.from_error))


class Application(_Application):
    """Extends wsgilib's application."""

    def __init__(self, *args, cors=False, debug=False, errorhandlers=(),
                 **kwargs):
        """Sets default error handlers."""
        super().__init__(
            *args, cors=cors, debug=debug,
            errorhandlers=ERROR_HANDLERS + errorhandlers, **kwargs)

I get W0235: Useless super delegation in method '__init__' (useless-super-delegation).
But if I refactor it to:

ERROR_HANDLERS = (
    (FieldValueError, data.FieldValueError.from_error),
    (FieldNotNullable, data.FieldNotNullable.from_error),
    (MissingKeyError, data.MissingKeyError.from_error),
    (InvalidKeys, data.InvalidKeys.from_error),
    (InvalidEnumerationValue, data.InvalidEnumerationValue.from_error))


class Application(_Application):
    """Extends wsgilib's application."""

    def __init__(self, *args, cors=False, debug=False, errorhandlers=(),
                 **kwargs):
        """Sets default error handlers."""
        errorhandlers = ERROR_HANDLERS + errorhandlers
        super().__init__(
            *args, cors=cors, debug=debug,
            errorhandlers=errorhandlers, **kwargs)

I do not get any warning.

Was this page helpful?
0 / 5 - 0 ratings