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

pylint doesn't understand the scope of except clause variables #626

Closed
pylint-bot opened this issue Aug 24, 2015 · 4 comments · Fixed by #4791
Closed

pylint doesn't understand the scope of except clause variables #626

pylint-bot opened this issue Aug 24, 2015 · 4 comments · Fixed by #4791
Labels
Bug 🪲 Control flow Requires control flow understanding
Milestone

Comments

@pylint-bot
Copy link

Originally reported by: Tom Dryer (BitBucket: tdryer, GitHub: @tdryer?)


In the following code, pylint fails to identify that the e in the except clause is an unused variable, and that the e in print(e) is an undefined variable:

#!python

def main():
    try:
        raise ValueError
    except ValueError as e:
        pass

    print(e)


main()

The issue appears to be that pylint doesn't understand that the scope of the exception variable is limited to the except clause.


@pylint-bot
Copy link
Author

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


The scope of 'e' is actually not limited to the except clause (try running your code, it's perfectly fine). Though pylint should issue 'used-before-assignment' because there will be a bug in case where the exception isn't raised.

@pylint-bot
Copy link
Author

Original comment by Tom Dryer (BitBucket: tdryer, GitHub: @tdryer?):


Oh, this seems to be specific to Python 3. In Python 2 the code works, but in 3.4 it will raise UnboundLocalError.

@pylint-bot
Copy link
Author

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


Oh right, I missed that part from https://www.python.org/dev/peps/pep-3110/

@pylint-bot
Copy link
Author

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):


This should definitely be fixed.

@PCManticore PCManticore added the Control flow Requires control flow understanding label Apr 13, 2016
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jul 30, 2021
david-yz-liu added a commit to david-yz-liu/pylint that referenced this issue Aug 2, 2021
david-yz-liu added a commit to david-yz-liu/pylint that referenced this issue Aug 2, 2021
david-yz-liu added a commit to david-yz-liu/pylint that referenced this issue Aug 2, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.10.0 milestone Aug 3, 2021
Pierre-Sassoulas pushed a commit that referenced this issue Aug 3, 2021
* Fix false negative for used-before-assignment (ExceptHandler)

Closes #626.

* Fix unused-variable check for exception variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Control flow Requires control flow understanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants