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 detection of undefined variable in try clause #3235

Closed
hemberger opened this issue Nov 7, 2019 · 1 comment
Closed

Inconsistent detection of undefined variable in try clause #3235

hemberger opened this issue Nov 7, 2019 · 1 comment
Labels

Comments

@hemberger
Copy link
Contributor

Steps to reproduce

When an undefined variable is in a try-clause, pylint does not report it as undefined, depending on which type of exception we're catching.

It fails to detect the undefined variable if the exception is bare or NameError:

try:
    bla
except:
    pass
try:
    bla
except NameError:
    pass

It correctly detects the undefined variable if the exception is Exception or some other derived exception that is not NameError:

try:
    bla
except Exception:
    pass
try:
    bla
except KeyError:
    pass

Current behavior

No error is raised (for bare exception and NameError)

Expected behavior

E0602: Undefined variable 'bla' (undefined-variable)

pylint --version output

pylint 2.4.3
astroid 2.3.3
Python 3.7.3 (default, Oct 7 2019, 12:56:13)
[GCC 8.3.0]

@PCManticore
Copy link
Contributor

Thanks for the report. For NameError this is expected, as pylint takes a hint from the exception handler in figuring out if the variable is expected to be undefined or not. For instance this is useful when wanting to check particular features of the language, like the presence of a builtin (in Python 2 vs Python 3 scenario for example).

But I think we should still emit it for bare except, and only allow NameError to swallow undefined variables.

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

No branches or pull requests

2 participants