You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classCustomException(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 its string representation is as follows:
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.
classCustomException(Exception):
def__init__(self, custom: int, arguments: str) ->None:
self.msg=f"Something specific happened with custom: {custom} and {arguments}"# <-- customsuper().__init__(self.msg)
Seems a good check for me and a bug waiting to happen. Please explain why this is bad to do in the README. And as always, provide good tests with correct code and bad code please.
Checking inheritance from any built-in exception is fairly straightforward, checking inheritance from any user exception is trickier - one approach is saving exception classes encountered previously in the file, another is to trigger on classes that inherit from *something, and the class name or the super() class name ends with "Exception[Group]" or "Error".
Oh and same goes for Warning classes, in case of -Werror
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 its string representation is as follows:and when the exception is instantiated with keyword arguments, we lose the information about the arguments' values.
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.Specification proposal
If a class:
Exception
,__str__()
method,__init__()
method doesn't enforce any positional-only argument,super().__init__()
in its__init__()
methodThen:
The text was updated successfully, but these errors were encountered: