pylint W0622 (Redefining built-in) when overriding "standard" methods in subclasses
Asked Answered
N

1

10

In Python, many methods define argument variables with "standardized" names, like:

def __exit__(self, type, value, traceback):

In the line above, variable type causes pylint to warn (W0622) that a built-in is being redefined: Redefining built-in 'type' (redefined-builtin).

There are many ways to fix this and make pylint happy (rename the variable, add a pylint directive (# pylint: disable=W0622) to ignore the problem, etc.).

What is the best/preferred/suggested/conventionally-used way (if any) to maintain a good code quality and make pylint happy in these cases?

Northerly answered 9/5, 2019 at 9:39 Comment(0)
P
3

It could be considered as a bad practise to disable pylint warning.

Quoting quantifiedcode.com :

In order for __exit__ to work properly it must have exactly three arguments: exception_type, exception_value, and traceback. The formal argument names in the method definition do not need to correspond directly to these names, but they must appear in this order.

As such a good option could be to use tuple packing def __exit__(self, *exc)

This what is suggested in this official documentation: https://docs.python.org/3/library/contextlib.html

Pelagian answered 11/12, 2019 at 14:20 Comment(1)
If I do this as is suggested, pylint gives a warning: "Parameters differ from overridden 'exit' method". So this is just replacing one pylint warning with another.Ideomotor

© 2022 - 2024 — McMap. All rights reserved.