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?

Starnuto di topo
  • 3,215
  • 5
  • 32
  • 66

1 Answers1

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

scoulomb
  • 630
  • 2
  • 7
  • 19
  • 3
    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. – Soeren D. Aug 21 '20 at 15:32