Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 651
samuelbrody1249 Avatar asked Jan 01 '26 01:01

samuelbrody1249


1 Answers

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.

like image 61
luther Avatar answered Jan 03 '26 13:01

luther



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!