-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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] Use a distinct type for module search paths in the module resolver #12379
Conversation
|
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 let @carljm approve this change. I like that some Arc<ModuleSearchPath>
are gone. I'm otherwise not quiet sure what the benefit is.
Nit: We can reduce the visibility of ModuleSearchPathBuf
and Ref
to pub(super)
.
pub(crate) fn extra(path: SystemPathBuf) -> Option<Self> { | ||
Some(Self(Arc::new(ModuleResolutionPathBuf::extra(path)?))) | ||
} | ||
|
||
pub(crate) fn first_party(path: SystemPathBuf) -> Option<Self> { | ||
Some(Self(Arc::new(ModuleResolutionPathBuf::first_party(path)?))) | ||
} | ||
|
||
pub(crate) fn custom_stdlib(path: &SystemPath) -> Option<Self> { | ||
Some(Self(Arc::new(ModuleResolutionPathBuf::standard_library( | ||
FilePath::System(path.join("stdlib")), | ||
)?))) | ||
} | ||
|
||
pub(crate) fn vendored_stdlib() -> Self { | ||
Self(Arc::new(ModuleResolutionPathBuf( | ||
ModuleResolutionPathBufInner::StandardLibrary(FilePath::vendored("stdlib")), | ||
))) | ||
} | ||
|
||
pub(crate) fn site_packages(path: SystemPathBuf) -> Option<Self> { | ||
Some(Self(Arc::new(ModuleResolutionPathBuf::site_packages( | ||
path, | ||
)?))) | ||
} | ||
|
||
pub(crate) fn editable(system: &dyn System, path: SystemPathBuf) -> Option<Self> { | ||
Some(Self(Arc::new( | ||
ModuleResolutionPathBuf::editable_installation_root(system, path)?, | ||
))) | ||
} |
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 think we should inline the
ruff/crates/red_knot_module_resolver/src/path.rs
Lines 171 to 212 in dc8ae36
#[must_use] | |
pub(crate) fn extra(path: impl Into<SystemPathBuf>) -> Option<Self> { | |
let path = path.into(); | |
path.extension() | |
.map_or(true, |ext| matches!(ext, "py" | "pyi")) | |
.then_some(Self(ModuleResolutionPathBufInner::Extra(path))) | |
} | |
#[must_use] | |
pub(crate) fn first_party(path: impl Into<SystemPathBuf>) -> Option<Self> { | |
let path = path.into(); | |
path.extension() | |
.map_or(true, |ext| matches!(ext, "pyi" | "py")) | |
.then_some(Self(ModuleResolutionPathBufInner::FirstParty(path))) | |
} | |
#[must_use] | |
pub(crate) fn standard_library(path: FilePath) -> Option<Self> { | |
path.extension() | |
.map_or(true, |ext| ext == "pyi") | |
.then_some(Self(ModuleResolutionPathBufInner::StandardLibrary(path))) | |
} | |
#[must_use] | |
pub(crate) fn site_packages(path: impl Into<SystemPathBuf>) -> Option<Self> { | |
let path = path.into(); | |
path.extension() | |
.map_or(true, |ext| matches!(ext, "pyi" | "py")) | |
.then_some(Self(ModuleResolutionPathBufInner::SitePackages(path))) | |
} | |
#[must_use] | |
pub(crate) fn editable_installation_root( | |
system: &dyn System, | |
path: impl Into<SystemPathBuf>, | |
) -> Option<Self> { | |
let path = path.into(); | |
// TODO: Add Salsa invalidation to this system call: | |
system | |
.is_directory(&path) | |
.then_some(Self(ModuleResolutionPathBufInner::EditableInstall(path))) | |
} |
and remove the definitions from ModuleResolutionPathBuf
. The methods on ModuleResolutionPathBuf
are only used in tests and I suspect that the tests should probably use ModuleResolutionPath
instead.
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 makes sense but I'm going to postpone it for now. The idea of this PR was that it would be a small, incremental change that would be easy to review. Addressing this would require lots of changes to tests, and I think this PR is an improvement over the status quo as it stands.
Yeah, it's harder to see the benefit when the validation changes are decoupled from the change to use a new type. This is partly why I originally put them together in one PR, in #12376. |
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 looks like a good start that brings a bit of extra naming clarity, but doesn't do the real refactor yet, just as advertised.
I agree with both of Micha's comments.
I'm fine landing this and holding off on any more refactor for now, until/unless there are actual problems caused by the current structure.
dc8ae36
to
245606a
Compare
Summary
This PR cleans up some of the settings validation and types in the module resolver. Namely, at various points in the crate, we use a type alias, a ModuleResolutionPathBuf or an
Arc<ModuleResolutionPathBuf>
to refer to a module search path. This PR goes some way to cleaning that up by introducing a newtype wrapper in path.rs for module-resolution paths that specifically represent search paths.The main motivation for this change is because module search paths and other module-resolution paths have different invariants, so should be validated differently. This PR has been split off from #12376, which bundled the validation changes in as the same PR, to ease review.
Copying from the PR description of #12376:
Test Plan
cargo test -p red_knot_module_resolver