Search code examples
pythonpython-3.xpylint

Unnecessary pass in defining exceptions


Often I will write a generic exception such as the following:

class MyException(Exception):
    "My custom exception."
    pass

This way I can check if that exception is the one I want in something like a try/except block. Yet pylint complains about this as follows:

unnecessary-pass: Unnecessary pass statement

What's the rationale behind this complaint? And is there a more preferred way to do the above? Even the python docs suggest using something like that for a user-defined exception:

class Error(Exception):
    """Base class for exceptions in this module."""
    pass

Solution

  • The rationale is that the string literal is a valid Python statement in the class body, therefore the pass is not needed to show indentation.

    This is a style issue, so there is no definitive answer for how to fix this. If you feel that the pass is useful, I suggest disabling that warning in pylint.