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
I have a number of code-bases (compilers) where I have a non-trivial error-handling scheme. But the pattern is pretty simple: I have a function that will always raise an exception. However it often creates false alarms as this reduced example shows:
#!/usr/bin/env python3
""" Example for nonreturning procedure """
class ParseError(Exception):
""" Reduced parse error class.
Would also contain file, line, column and context information """
def __init__(self, msg):
self.msg = msg
def error(message):
""" Raise parse error
The real function would also compute extra information such as current
line and column to add to the exception. """
raise ParseError(message)
def example(potato):
""" Example """
if potato > 0:
return potato
else:
error("potato is not positive")
When running pylint, you get this message on example:
foo.py:22:0: R1710: Either all return statements in a function should return an
expression, or none of them should. (inconsistent-return-statements)
The reason I am not directly raising an exception here is that the error function is actually quite complex, and it is not code that should be repeated all over the parsers. Ideally a @noreturn annotation would exist in Python, but maybe something else can be done for PyLint?
Right now I have to justify a bunch of code, but this is not very nice.
Thanks in advance for any improvement on this topic!
The text was updated successfully, but these errors were encountered:
As an alternative, you can use error() as a constructor and raise it's return as in raise error(....). Fixing this would require pylint to inspect any considered function call and see if it's raising an exception or not, which is doable but adds extra capabilities on top of this check. I'm not sure if it's that big of a problem.
I have a number of code-bases (compilers) where I have a non-trivial error-handling scheme. But the pattern is pretty simple: I have a function that will always raise an exception. However it often creates false alarms as this reduced example shows:
When running pylint, you get this message on example:
The reason I am not directly raising an exception here is that the error function is actually quite complex, and it is not code that should be repeated all over the parsers. Ideally a
@noreturn
annotation would exist in Python, but maybe something else can be done for PyLint?Right now I have to justify a bunch of code, but this is not very nice.
Thanks in advance for any improvement on this topic!
The text was updated successfully, but these errors were encountered: