[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
| 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.
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.
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
carljm
left a comment
There was a problem hiding this comment.
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.pymodule:and a
bar.pymodule has this:then we will now recognize
aas bound inbar.py. We don't yet revealboolas the inferred type, but that depends on fixing astral-sh/ty#220.Test Plan
Assertions in existing mdtests are modified, and some new assertions are added