[used before def] improve handling of global definitions in local scopes#14517
[used before def] improve handling of global definitions in local scopes#14517JukkaL merged 14 commits intopython:masterfrom
Conversation
# Conflicts: # mypy/partially_defined.py # test-data/unit/check-possibly-undefined.test
| Class = 2 | ||
| Func = 3 | ||
| Generator = 3 | ||
| Generator = 4 |
There was a problem hiding this comment.
This didn't matter until we needed to handle generators differently. Generators actually do inherit the scope!
| x = z # E: Name "z" is used before definition | ||
| z: int = 2 | ||
|
|
||
| [case testUsedBeforeDefImportsBasic] |
There was a problem hiding this comment.
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.
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.
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. 🤖🎉 |
JukkaL
left a comment
There was a problem hiding this comment.
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
cwe have a builtin).Fixes #14213 (as much as is reasonable to do)