-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[red-knot] Recognize an assignment in a function/class scopes as a global-scope definition for purposes of *
imports if the assigned symbol is declared global
#16959
Conversation
global
*
imports if the assigned symbol is declared global
|
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
} | ||
} | ||
|
||
fn possibly_add_export(&mut self, name: &Name) { | ||
if name.starts_with('_') { | ||
return; | ||
} | ||
if self.scope_kind.is_not_global() && !self.global_declarations_in_scope.contains(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 don't know how hot this code path is but we could consider using hashbrown's hash table to only compute the hash key once (for global_declarations_in_scope
and exports
). But that's probably a premature optimization :)
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'll leave this for now (following our in-person conversation) but happy to look into this later if it shows up in profiling etc.!
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
498eb5e
to
42ecd9a
Compare
…obal-scope definition if the assigned symbol is declared `global`
42ecd9a
to
ed5678c
Compare
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.
Sorry for not getting to clarity on this sooner, but I feel like we shouldn't support this. While this PR doesn't add a lot of complexity, fully supporting this pattern will, because it means that the types of symbols in an outer scope can depend on an arbitrary nested scope.
I think it is reasonable to require that if you use global
in a function and assign to that name, the name must explicitly exist in the global scope.
If we do end up deciding we have to support this pattern, I want to add full support for it altogether (that is, not do star-import support for it separately from actual type inference support) so we can see the full picture of what it requires.
yes, after discussing this with you the other day, I agree that we probably shouldn't do this right now. Let's add support for |
Summary
Further work towards #14169. If something like this exists in a
foo.py
module:and a
bar.py
module has this:then we will now recognize
a
as bound inbar.py
. We don't yet revealbool
as the inferred type, but that depends on fixing #15385.Test Plan
Assertions in existing mdtests are modified, and some new assertions are added