mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
Nested namespace packages support (#10541)
## Summary PEP 420 says [nested namespace packages](https://peps.python.org/pep-0420/#nested-namespace-packages) are allowed, i.e. marking a directory as a namespace package marks all subdirectories in the subtree as namespace packages. `is_package` is modified to use `Path::starts_with` and the order of checks is reversed to do in-memory checks first before hitting the disk. ## Test Plan Added unit tests. Previously all tests were run with `namespace_packages == &[]`. Verified that one of the tests was failing before changing the implementation. ## Future Improvements The `is_package_with_cache` can probably be rewritten to avoid repeated calls to `Path::starts_with`, by caching all directories up to the `namespace_root`: ```ruff let namespace_root = namespace_packages .iter() .filter(|namespace_package| path.starts_with(namespace_package)) .min(); ```
This commit is contained in:
parent
9856c1446b
commit
d625f55c05
5 changed files with 48 additions and 16 deletions
|
@ -293,8 +293,8 @@ pub struct Options {
|
|||
pub builtins: Option<Vec<String>>,
|
||||
|
||||
/// Mark the specified directories as namespace packages. For the purpose of
|
||||
/// module resolution, Ruff will treat those directories as if they
|
||||
/// contained an `__init__.py` file.
|
||||
/// module resolution, Ruff will treat those directories and all their subdirectories
|
||||
/// as if they contained an `__init__.py` file.
|
||||
#[option(
|
||||
default = r#"[]"#,
|
||||
value_type = "list[str]",
|
||||
|
|
|
@ -203,7 +203,7 @@ impl<'a> Resolver<'a> {
|
|||
/// A wrapper around `detect_package_root` to cache filesystem lookups.
|
||||
fn detect_package_root_with_cache<'a>(
|
||||
path: &'a Path,
|
||||
namespace_packages: &'a [PathBuf],
|
||||
namespace_packages: &[PathBuf],
|
||||
package_cache: &mut FxHashMap<&'a Path, bool>,
|
||||
) -> Option<&'a Path> {
|
||||
let mut current = None;
|
||||
|
@ -219,7 +219,7 @@ fn detect_package_root_with_cache<'a>(
|
|||
/// A wrapper around `is_package` to cache filesystem lookups.
|
||||
fn is_package_with_cache<'a>(
|
||||
path: &'a Path,
|
||||
namespace_packages: &'a [PathBuf],
|
||||
namespace_packages: &[PathBuf],
|
||||
package_cache: &mut FxHashMap<&'a Path, bool>,
|
||||
) -> bool {
|
||||
*package_cache
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue