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

False negative undefined-variable after a caught exception shadowing a variable name #157

Open
pylint-bot opened this issue Mar 7, 2014 · 2 comments
Labels
C: undefined-variable Issues related to 'undefined-variable' check Control flow Requires control flow understanding False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@pylint-bot
Copy link

pylint-bot commented Mar 7, 2014

Originally reported by: BitBucket: moxian, GitHub: @moxian?


Pylint'ing snippet

def main():
    exc = None
    try:
        [][1] = 12
    except IndexError as exc:
        pass
    if exc is not None:
        raise exc


main()

says

Raising NoneType while only classes, instances or string are allowed (raising-bad-type)

when in fact None will never be attempted to be raised

Python 2


@pylint-bot
Copy link
Author

Original comment by Sylvain Thénault (BitBucket: sthenault, GitHub: @sthenault?):


pylint has a somewhat dumb static inference system which doesn't handle flow handling. Also in your case I suspect the code could be rewritten in a way both understandable by pylint and more readable, but we would need more context for that.

@PCManticore PCManticore added this to the 2.0 milestone May 10, 2016
@PCManticore PCManticore modified the milestones: 4K, 3.0 Jun 17, 2016
@PCManticore PCManticore added the Control flow Requires control flow understanding label Aug 4, 2016
@PCManticore PCManticore changed the title False-positive on raising None raising-non-exception false positive Feb 18, 2019
@PCManticore PCManticore removed this from the 4.0 milestone Sep 26, 2019
@Pierre-Sassoulas
Copy link
Member

Given:

# pylint: disable=missing-docstring


def main():
    exc = None
    print(exc)
    try:
        [][1] = 12
    except IndexError as exc:
        print(exc)
    if exc is not None:
        raise exc


main()

Pylint does not raise any errors but running the code gives:

None
list assignment index out of range
Traceback (most recent call last):
  File "a.py", line 15, in <module>
    main()
  File "a.py", line 11, in main
    if exc is not None:
UnboundLocalError: local variable 'exc' referenced before assignment

So it actually became a false negative for undefined variable in python 3.

@Pierre-Sassoulas Pierre-Sassoulas changed the title raising-non-exception false positive False negative undefined-variable after a caught exception shadowing a variable name Jun 14, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the False Negative 🦋 No message is emitted but something is wrong with the code label Jun 14, 2021
@DanielNoord DanielNoord added the C: undefined-variable Issues related to 'undefined-variable' check label Nov 18, 2021
@pylint-dev pylint-dev deleted a comment from pylint-bot Jul 4, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Bug 🪲 labels Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: undefined-variable Issues related to 'undefined-variable' check Control flow Requires control flow understanding False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

4 participants