Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request for nonreturning procedures #3188

Closed
florianschanda opened this issue Oct 14, 2019 · 1 comment
Closed

Feature request for nonreturning procedures #3188

florianschanda opened this issue Oct 14, 2019 · 1 comment

Comments

@florianschanda
Copy link

florianschanda commented Oct 14, 2019

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!

@PCManticore
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants