-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Detect variables that are used before they're defined #14163
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
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good, but please address two comments before landing this.
"use-before-def", | ||
"Warn about variables that are used before they are defined", | ||
"General", | ||
default_enabled=False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make this one enabled by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we need to understand the performance of the definedness analysis before enabling it by default. Also the implementation is still somewhat incomplete, and it would be good to fix known issues first.
In particular, the current implementation of try statements (#14114, currently not merged) seems to be exponential which might become expensive. Perhaps there is a more efficient way to do the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We all agreed that always-undefined should be enabled by default in 1.0. Then there is no point in postponing this now, the sooner we enable this, the sooner we get some feedback, and will be able to fix possible problems. We will at least get feedback from mypy_primer
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A follow-up PR can be immediately created after this has been merged that turns this on by default to get feedback from mypy_primer. We don't need to wait until everything is complete (but we may want to wait for some additional changes before merging the PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, let's do it this way then.
pass | ||
else: | ||
y = z # E: Name "z" is used before definition | ||
x = z # No error here -- we shouldn't report the error twice. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should actually report both cases. Imagine you have
def some_long_func() -> None:
x = z
# 50 lines later
y = z
In this case someone will need to fix first error, then run mypy again to get the second one and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may be a bit cleaner to just report one error but let's report all and then see what real-world issues we see with it.
# Conflicts: # test-data/unit/check-partially-defined.test
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This enables the error code added in #14163.
This implements a check when a variable is defined before use. Something like:
I've combined this check with the partially defined check but added a separate error code. It's probably worth cleaning it up and making the separation between the two checks a bit more clear. I can do this in a follow-up PR.
Fixes #14124