-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Labels
ruleImplementing or modifying a lint ruleImplementing or modifying a lint rule
Description
class CustomException(Exception):
def __init__(self, custom: int, arguments: str) -> None:
self.msg = f"Something specific happened with custom: {custom} and {arguments}"
Above exception class has an issue, in my personal experience it's quite common. The implementer assumes, that they will use the self.msg
parameter during exception handling. The issue araises when the exception gets anywhere else as the its string representation is as follows:
>>> raise CustomException(42, 'test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.CustomException: (42, 'test')
and when the exception is instantiated with keyword arguments, we lose the information about the arguments' values.
>>> raise CustomException(custom=1, arguments='two')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.CustomException
How to fix it? By calling super().__init__()
in the constructor. That way the string representation of the custom exception will contain the error message.
class CustomException(Exception):
def __init__(self, custom: int, arguments: str) -> None:
self.msg = "Something specific happened with custom: {custom} and {arguments}"
super().__init__(self.msg)
>>> raise CustomException(1, 'two')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.CustomException: Something specific happened with custom: 1 and two
>>> raise CustomException(custom=1, parameters='two')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.CustomException: Something specific happened with custom: 1 and two
Specification proposal
If a class:
- inherits from from
Exception
, - doesn't implement own
__str__()
method, - its
__init__()
method doesn't enforce any positional-only argument, - and doesn't call
super().__init__()
in its__init__()
method
Then:
- new rule should raise an error, calling to fix it.
ekamil, szymon-borkowski and Finkregh
Metadata
Metadata
Assignees
Labels
ruleImplementing or modifying a lint ruleImplementing or modifying a lint rule