You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PEP 420 says 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:
let namespace_root = namespace_packages
.iter()
.filter(|namespace_package| path.starts_with(namespace_package))
.min();
Did you run into an issue with this in practice that motivated this change?
We have a monorepo where all the files are located in an organization/project/subproject/ structure and both organization/ and organization/project/ directories are meant to be namespace packages, so no __init__.py in them.
We wanted to use banned-api (TID251), but for python sources it detected subproject as python package name. We didn't want to mark all the organization/project/ directories as namespace packages (it would be brittle, as more projects might be added without changing the ruff config), but marking just organization/ as a namespace package didn't work.
This test illustrates our situation pretty well. Currently ruff assigns package name core to this directory, but we want it to be recognized as python_modules.core.core with only python_modules being marked as namespace package.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PEP 420 says nested namespace packages are allowed, i.e. marking a directory as a namespace package marks all subdirectories in the subtree as namespace packages.
is_packageis modified to usePath::starts_withand 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_cachecan probably be rewritten to avoid repeated calls toPath::starts_with, by caching all directories up to thenamespace_root: