Skip to content

Unused variable in except clauses differs between py2 and py3 #301

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

Closed
moodh opened this issue Oct 9, 2017 · 8 comments
Closed

Unused variable in except clauses differs between py2 and py3 #301

moodh opened this issue Oct 9, 2017 · 8 comments

Comments

@moodh
Copy link

moodh commented Oct 9, 2017

The following code gives a warning in py2 but not py3.
But only if the try clause is inside a function, otherwise it passes on both.

test.py:5:25: F841 local variable 'e' is assigned to but never used

def test():
    try:
        raise Exception('foo')
    except Exception as e:
        pass

Is this a bug or a new feature? Could not find anything related to this "regression" but currently py3 does not catch issues py2 would have caught. This is a problem when writing compatible code at the very least.

Could be related to this PR: #59 (found here https://github.com/PyCQA/pyflakes/blob/master/pyflakes/checker.py#L1325)

@myint
Copy link
Member

myint commented Oct 10, 2017

This expected. Python 3 clears the variable while Python 2 does not.

https://stackoverflow.com/questions/29268892/python-3-exception-deletes-variable-in-enclosing-scope-for-unknown-reason#29268974

@myint myint closed this as completed Oct 10, 2017
@moodh
Copy link
Author

moodh commented Oct 13, 2017

@myint: I understand it's safer than previous versions, but isn't it useful to catch unused code?

@simonpercivall
Copy link

@myint Apart from being inconsistent; and apart from it actually being unused, it's still IMO a good bug-catching check, because:

def test():
    e = ''

    try:
        raise Exception('foo')
    except Exception as e:
        pass

    print(e)

does cause an UnboundLocalError in Py3.

@myint
Copy link
Member

myint commented Oct 13, 2017

Hmm, I think I read the link to #59, but forgot read the actual example @moodh provided.

@myint myint reopened this Oct 13, 2017
@myint
Copy link
Member

myint commented Oct 19, 2017

Does #293 fix it for you?

@moodh
Copy link
Author

moodh commented Oct 19, 2017

Yes, the original bug we found is now caught by your fix, thanks!

@myint
Copy link
Member

myint commented Oct 19, 2017

@jdufresne actually fixed this in #293 a month prior. 😄

@jdufresne
Copy link
Contributor

Thanks for testing. I updated the PR to reference this issue.

@myint myint closed this as completed in ef6f438 Oct 21, 2017
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

4 participants