mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
When checking module visibility, don't check entire ancestry (#3835)
This commit is contained in:
parent
d2f2544f6e
commit
849091d846
11 changed files with 50 additions and 19 deletions
|
@ -63,6 +63,7 @@ impl<'a> Context<'a> {
|
|||
path: &'a Path,
|
||||
module_path: Option<Vec<String>>,
|
||||
) -> Self {
|
||||
let visibility = module_visibility(module_path.as_deref(), path);
|
||||
Self {
|
||||
typing_modules,
|
||||
module_path,
|
||||
|
@ -79,7 +80,7 @@ impl<'a> Context<'a> {
|
|||
body_index: 0,
|
||||
visible_scope: VisibleScope {
|
||||
modifier: Modifier::Module,
|
||||
visibility: module_visibility(path),
|
||||
visibility,
|
||||
},
|
||||
in_annotation: false,
|
||||
in_type_definition: false,
|
||||
|
|
|
@ -131,28 +131,24 @@ fn stem(path: &str) -> &str {
|
|||
}
|
||||
|
||||
/// Return the `Visibility` of the Python file at `Path` based on its name.
|
||||
pub fn module_visibility(path: &Path) -> Visibility {
|
||||
let mut components = path.iter().rev();
|
||||
|
||||
// Is the module itself private?
|
||||
// Ex) `_foo.py` (but not `__init__.py`)
|
||||
if let Some(filename) = components.next() {
|
||||
let module_name = filename.to_string_lossy();
|
||||
let module_name = stem(&module_name);
|
||||
if is_private_module(module_name) {
|
||||
pub fn module_visibility(module_path: Option<&[String]>, path: &Path) -> Visibility {
|
||||
if let Some(module_path) = module_path {
|
||||
if module_path.iter().any(|m| is_private_module(m)) {
|
||||
return Visibility::Private;
|
||||
}
|
||||
}
|
||||
|
||||
// Is the module in a private parent?
|
||||
// Ex) `_foo/bar.py`
|
||||
for component in components {
|
||||
let module_name = component.to_string_lossy();
|
||||
if is_private_module(&module_name) {
|
||||
return Visibility::Private;
|
||||
} else {
|
||||
// When module_path is None, path is a script outside a package, so just
|
||||
// check to see if the module name itself is private.
|
||||
// Ex) `_foo.py` (but not `__init__.py`)
|
||||
let mut components = path.iter().rev();
|
||||
if let Some(filename) = components.next() {
|
||||
let module_name = filename.to_string_lossy();
|
||||
let module_name = stem(&module_name);
|
||||
if is_private_module(module_name) {
|
||||
return Visibility::Private;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Visibility::Public
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue