[red-knot] support implicit global name lookups#12374
Merged
Conversation
CodSpeed Performance ReportMerging #12374 will degrade performances by 10.44%Comparing Summary
Benchmarks breakdown
|
Contributor
|
Contributor
Author
|
As expected, this gives back the wins (and a bit more) that we saw previously when switching to per-definition inference and removing this lookup to other scopes. Not only are we now doing extra work on names that are global references (and actually also wrongly on names that are arguments, because we currently don't create definitions for arguments), but that means we actually now recognize the decorator and run the lint rule, so we're just doing a lot more work in the benchmark than before. |
MichaReiser
approved these changes
Jul 18, 2024
AlexWaygood
reviewed
Jul 18, 2024
f341f70 to
aa4222d
Compare
611d062 to
a5cedb7
Compare
a5cedb7 to
b2b4bd7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support falling back to a global name lookup if a name isn't defined in the local scope, in the cases where that is correct according to Python semantics.
In class scopes, a name lookup checks the local namespace first, and if the name isn't found there, looks it up in globals.
In function scopes (and type parameter scopes, which are function-like), if a name has any definitions in the local scope, it is a local, and accessing it when none of those definitions have executed yet just results in an
UnboundLocalError, it does not fall back to a global. If the name does not have any definitions in the local scope, then it is an implicit global.Public symbol type lookups never include such a fall back. For example, if a name is not defined in a class scope, it is not available as a member on that class, even if a name lookup within the class scope would have fallen back to a global lookup.
This PR makes the
@overridelint rule work again.Not yet included/supported in this PR:
globalandnonlocalstatements, which force a symbol to be treated as global or nonlocal even if it has definitions in the local scope.I would like to expose nicer APIs for the various kinds of symbols (explicit global, implicit global, free, etc), but this will also wait for a later PR, when more kinds of symbols are supported.