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

Extend possibly-used-before-assignment to definitions in try then used in finally or after #2835

Open
lbenezriravin opened this issue Mar 25, 2019 · 5 comments
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check 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

@lbenezriravin
Copy link
Contributor

Potentially related to #2615. Not sure if these would both be fixed by the same patch, so I'm making a new issue.

Is your feature request related to a problem? Please describe

The following code passes pylint:

try:
   foo = 1 / 0
except:
   pass
print(foo)

But raises an UnboundLocalError when run.

Describe the solution you'd like

Pylint already has an undefined-loop-variable warning for a similar case that could result in the same error:

for foo in []:
    pass
print(foo)

I feel that the former code should raise a warning if that latter code does. More generally, I don't expect UnboundLocalError from any code that passes pylint, but I'm not sure if that guarantee is possible.

Additional context

Thanks for your work on this tool!

@PCManticore
Copy link
Contributor

Thanks @lbenezriravin This makes sense.

@Pierre-Sassoulas
Copy link
Member

Closing as duplicate of #5524

@DanielNoord
Copy link
Collaborator

This was actually not a duplicate see: #5764 (comment).

A very relevant comment was made by @cdce8p in #5524 here:

Another idea. I like how pyright names it. Maybe possibly-unbound?

"a" is possibly unbound

Maybe consider-using-try-else

We shouldn't really "force" users to add an else case. There are just to many alternatives. You could for example assign a default value before you enter the try block.

@DanielNoord DanielNoord reopened this Feb 8, 2022
@Pierre-Sassoulas Pierre-Sassoulas removed the Duplicate 🐫 Duplicate of an already existing issue label Feb 8, 2022
@jacobtylerwalls
Copy link
Member

Implementation tip: when checking whether the except handlers define a name, raise, or return, consider using NamesConsumer._defines_name_raises_or_returns() from #5764

@Pierre-Sassoulas
Copy link
Member

pyright has possibly unbound, mypy has possibly-undefined, I think we should introduce either one of those two names in pylint on top of undefined-variable (maybe possibly-undefined-variable for consistency)

@jacobtylerwalls jacobtylerwalls changed the title Catch potential UnboundLocalError in try/except Extend possibly-used-before-assignment to definitions in try then used in finally or after Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check 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

5 participants