-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Ruff (and flake8) doesn't report a situation where a variable _may_ be unbound #2493
Comments
Appreciate the clear issue! Right now, (1) is outside of our capabilities -- although I'm surprised that Mypy doesn't flag this... Pyright does:
We could potentially include some kind of "possibly unbound" code for those cases. I'd have to think on it 🤔 |
And me! For cross-referencing, it seems like the relevant issues are |
I'm gonna close for now just because it's not super actionable. I'd like Ruff to be able to handle cases like this eventually, but it'd be a fairly big project and probably outside of the scope of a bug fix. Hope that intent is clear. I appreciate the issue! |
just wanted to drop in and say that I would find this extremely useful it's really easy in long Python functions and code blocks when refactoring and moving variables inside nested conditionals or inline functions to nest a variable used somewhere else and only discover at runtime via a Sentry notification days later |
Is this still unsupported? |
STILL ~ |
Note: this is the subject of this issue: #6902
It sounds like a pretty tough one, but I'm sure Ruff maintainers would welcome a PR for it. |
Consider the following snippet:
There are two problems with this snippet.
maybe_runtime_error
, the namex
may not be bound to a variable when we come to evaluatelist(x)
. This means we might get a runtime UnboundLocalError.runtime_error
, the namey
is never bound to a variable when we come to evaluatelist(y)
. We'll always get a runtime UnboundLocalError when calling this function.Problem (2) is spotted by ruff, but problem (1) is not.
I was a little surprised by this! But flake8 has the same output:
I'm not sure if it's fair game for ruff to spot the problem with
x
---perhaps that's more the remit of a typechecker like mypy. (Though mypy didn't warn about this either when I tried it!) It'd be great if it was feasible to spot this at lint-time.Configuration
And as for the version:
The text was updated successfully, but these errors were encountered: