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

inconsistent-return-statements not working in try branches #3468

Closed
joshuahaertel opened this issue Apr 1, 2020 · 7 comments · Fixed by #3782
Closed

inconsistent-return-statements not working in try branches #3468

joshuahaertel opened this issue Apr 1, 2020 · 7 comments · Fixed by #3782
Assignees
Labels

Comments

@joshuahaertel
Copy link

Steps to reproduce

Add a return in the try clause

def foo(bar):
    try:
        return bar.baz
    except AttributeError:
        pass

Current behavior

Pylint emits no warning for inconsistent-return-statements. Note that it will emit a warning if the value is returned in the except clause.

Expected behavior

Pylint should emit a warning for not explicitly returning a value if an exception is hit, be it in the except clause itself or at the end of the call.

pylint --version output

$ pylint --version output
pylint 2.4.4
astroid 2.3.3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]
@joshuahaertel
Copy link
Author

Also worth noting that returning in some except clauses and not in others does not emit a warning either.

def foo(bar):
    try:
        return bar.baz
    except AttributeError:
        pass
    except KeyError:
        return True

@PCManticore
Copy link
Contributor

PCManticore commented Apr 3, 2020

Does the error reproduce for you for these two examples or is there additional code that's needed for reproduction? Also tested with the unreleased 2.5.0 from the master branch and can't reproduce it there as well.

Actually I'm dumb, I misinterpreted what the issue was about, the error should be emitted.

@same-id
Copy link

same-id commented Feb 23, 2021

I think this now introduces a different bug

try:
  n = int(s)
  if n < 1:
    raise ValueError()
  return n
except ValueError:
  parser.error('parser error') # calls system exit

even if I replace parser.error() with an explicit function that is marked with -> typing.NoReturn as suggested in #4122 is still a false positive.

The only solution now is to preinitialize:

n = None
try:
  n = int(s)
  if n < 1:
    raise ValueError()
except ValueError:
  parser.error('parser error') # calls system exit
return n

Which isn't that great since it requires moving n to a less confined scope

yagebu pushed a commit to lektor/lektor that referenced this issue Feb 24, 2021
…ge of CI tests (#891)

* Fix for inconsistent-return-statements errors from new pylint (2.7)

References:
- https://github.com/PyCQA/pylint/blob/5e04ce74faa0142fe3ff99eec08479cfaeb9584c/ChangeLog#L104
- pylint-dev/pylint#3468

* Pin pylint so that new releases do not spontaneously break CI tests
* Another "fix" for what, I assume, is a recent change in pylint behavior
@hippo91
Copy link
Contributor

hippo91 commented Mar 3, 2021

@same-id thanks for the report. I can reproduce. Can you open a new issue for this please?

@hippo91
Copy link
Contributor

hippo91 commented Mar 3, 2021

By the way another solution seems to add a return statement just before the parser.error call.

@hippo91
Copy link
Contributor

hippo91 commented Mar 5, 2021

@same-id i opened #4188

@same-id
Copy link

same-id commented Mar 7, 2021

Thanks!

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

Successfully merging a pull request may close this issue.

4 participants