-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] Add initial support for *
imports
#16923
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
Conversation
…`DefinitionKind` for star imports
829b35c
to
d87cd7c
Compare
|
This comment was marked as resolved.
This comment was marked as resolved.
crates/red_knot_python_semantic/src/semantic_index/definition.rs
Outdated
Show resolved
Hide resolved
9752faa
to
4e6f758
Compare
I figured out the bug here and fixed it. Unfortunately, fixing the bug completely broke all our special casing for the builtin There were a huge amount of test failures in |
|
9479f00
to
550a937
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.
Nice work!
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
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 is great!
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/src/semantic_index/re_exports.rs
Outdated
Show resolved
Hide resolved
(haven't finished addressing/responding to review comments yet, will continue tomorrow!) |
let Some(module) = resolve_module(self.db, &module_name) else { | ||
continue; | ||
}; |
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'm wondering if we should cache the resolved module in the DefinitionKind
for *
import definitions? It feels slightly silly to re-resolve the module name and module for these definitions in infer.rs
, when we know that we wouldn't have created any definitions for the node during semantic indexing unless the module name and module were both resolvable.
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.
Hrm, this involves quite an annoying amount of refactoring unless we also cache the resolved ModuleName
as well as the File
on StarImportDefinitionKind
. Leaning towards leaning this as-is for now, even though the duplication of logic between this file and infer.rs
is somewhat annoying.
7a6bc73
to
92d50f1
Compare
92d50f1
to
ca5184f
Compare
crates/red_knot_python_semantic/resources/mdtest/import/star.md
Outdated
Show resolved
Hide resolved
…rts should only have 0 or 1 definition(s) associated with them
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.
Looks good to me!
crates/red_knot_python_semantic/resources/mdtest/import/star.md
Outdated
Show resolved
Hide resolved
## Summary This PR separates out the entirely new tests from #16923 into a standalone PR. I'll rebase #16923 on top of this branch. The reasons for separating it out are: - It should make it clearer to see in <#16923> exactly how the functionality is changing (we can see the assertions in the tests _change_, which isn't so obvious if the tests are entirely new) - The diff on <#16923> is getting pretty big; this should reduce the diff on that PR somewhat - These tests seem useful in and of themselves, so even if we need to do a wholesale revert of <#16923> for whatever reason, it'll be nice to keep the tests ## Test Plan `cargo test -p red_knot_python_semantic`
Summary
This PR adds initial support for
*
imports to red-knot. The approach is to implement a standalone query, called from semantic indexing, that visits the module referenced by the*
import and collects all global-scope public names that will be imported by the*
import. TheSemanticIndexBuilder
then adds separate definitions for each of these names, all keyed to the sameast::Alias
node that represents the*
import.There are many pieces of
*
-import semantics that are still yet to be done, even with this PR:This PR does not attempt to implement any of the semantics to do with
__all__
. (If a module defines__all__
, then only the symbols included in__all__
are imported, not all public global-scope symbols.With the logic implemented in this PR as it currently stands, we sometimes incorrectly consider a symbol bound even though it is defined in a branch that is statically known to be dead code, e.g. (assuming the target Python version is set to 3.11):
Implementing these features is important, but is for now deferred to followup PRs.
Many thanks to @ntBre, who contributed to this PR in a pairing session on Friday!
Test Plan
Assertions in existing mdtests are adjusted, and several new ones are added.
TODO: the(I figured out the bug here and fixed it!)collections.abc
integration test at the bottom ofstar.md
is still failing. Not sure why...