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

Not catching use of undefined variables in except clauses #2615

Closed
smontanaro opened this issue Nov 28, 2018 · 1 comment · Fixed by #5384
Closed

Not catching use of undefined variables in except clauses #2615

smontanaro opened this issue Nov 28, 2018 · 1 comment · Fixed by #5384
Labels
Bug 🪲 C: undefined-variable Issues related to 'undefined-variable' check
Milestone

Comments

@smontanaro
Copy link

This report is similar to others which mention undetected use of undefined variables after if False or in the finally branch of a try/finally statement. I think use of undefined variables in except clauses is perhaps a higher priority fix. Consider this function:

def func(url, headers):
    "doc"
    try:
        resp = requests.get(url, headers)
    except requests.exceptions.SSLError:
        print("SSL error - headers %s", resp.request.headers)

It's clear that the variable resp is used while undefined. Missing it is more critical than in most other cases because it is generally going to be executed infrequently. If, as in this case, only one variable is assigned in the try clause and it wasn't defined beforehand, then an undefined-variable message should be emitted. If more than one variable is assigned in the try clause, then a possibly-undefined-variable message should go out over the wire. I'm not suggesting that pylint should be able to do any kind of sophisticated analysis. If it wasn't defined before the try clause, is assigned in the try clause, then referenced in the except clause, it should be flagged.

If the user assigns 37 different variables in the try clause and references fourteen of them in the except clause, the voluminous amount of output will also serve as a good clue that they need to tighten up their try clause (quoting from PEP 8: "Additionally, for all try/except clauses, limit the try clause to the absolute minimum amount of code necessary.") :-)

@PCManticore
Copy link
Contributor

Thanks @smontanaro ! we definitely should fix that.

@DanielNoord DanielNoord added the C: undefined-variable Issues related to 'undefined-variable' check label Nov 18, 2021
jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Nov 26, 2021
Also fix bug where different TryFinally nodes were mixed up
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Dec 11, 2021
jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Dec 11, 2021
…final or except blocks where try statements could have failed
Pierre-Sassoulas pushed a commit that referenced this issue Dec 13, 2021
…atements could have failed (#5384)

* Emit `used-before-assignment` in final or except blocks where try statements could have failed

Fix #85, #2615
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 C: undefined-variable Issues related to 'undefined-variable' check
Projects
None yet
4 participants