-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[used before def] improve handling of global definitions in local scopes #14517
Conversation
# Conflicts: # mypy/partially_defined.py # test-data/unit/check-possibly-undefined.test
@@ -171,7 +173,7 @@ class ScopeType(Enum): | |||
Global = 1 | |||
Class = 2 | |||
Func = 3 | |||
Generator = 3 | |||
Generator = 4 |
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.
This didn't matter until we needed to handle generators differently. Generators actually do inherit the scope!
@@ -829,67 +873,56 @@ def f4() -> None: | |||
x = z # E: Name "z" is used before definition | |||
z: int = 2 | |||
|
|||
[case testUsedBeforeDefImportsBasic] |
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.
when I wrote these import tests, I misunderstood how they would actually behave at runtime.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mypy/partially_defined.py
Outdated
if builtins_mod: | ||
assert isinstance(builtins_mod.node, MypyFile) | ||
for name in builtins_mod.node.names: | ||
self.tracker.record_definition(name) |
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 wonder if this might degrade performance, since builtins have over 200 definitions, and it looks like we are iterating over all of them for every module. Can you measure the time spent in this loop when performing a self check, for example?
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 have found a way to deal with this that doesn't involve calling record_definition
every time! So performance should be unchanged.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
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.
LGTM. Thanks for making this faster!
While working on #14483, we discovered that variable inheritance didn't work quite right. In particular, functions would inherit variables from outer scope. On the surface, this is what you want but actually, they only inherit the scope if there isn't a colliding definition within that scope.
Here's an example:
This PR also fixes issues with builtins (exactly the same example as above but instead of
c
we have a builtin).Fixes #14213 (as much as is reasonable to do)