mirror of https://github.com/astral-sh/ruff
When checking module visibility, don't check entire ancestry (#3835)
This commit is contained in:
parent
d2f2544f6e
commit
849091d846
0
crates/ruff/resources/test/fixtures/pydocstyle/_unrelated/pkg/_priv/__init__.py
vendored
Normal file
0
crates/ruff/resources/test/fixtures/pydocstyle/_unrelated/pkg/_priv/__init__.py
vendored
Normal file
0
crates/ruff/resources/test/fixtures/pydocstyle/_unrelated/pkg/_priv/no_D100_priv.py
vendored
Normal file
0
crates/ruff/resources/test/fixtures/pydocstyle/_unrelated/pkg/_priv/no_D100_priv.py
vendored
Normal file
|
|
@ -60,6 +60,9 @@ mod tests {
|
|||
#[test_case(Rule::UndocumentedPublicMethod, Path::new("D.py"); "D102_0")]
|
||||
#[test_case(Rule::UndocumentedPublicMethod, Path::new("setter.py"); "D102_1")]
|
||||
#[test_case(Rule::UndocumentedPublicModule, Path::new("D.py"); "D100")]
|
||||
#[test_case(Rule::UndocumentedPublicModule, Path::new("_unrelated/pkg/D100_pub.py"); "D100_ignore_unrelated_pub")]
|
||||
#[test_case(Rule::UndocumentedPublicModule, Path::new("_unrelated/pkg/_priv/no_D100_priv.py"); "no_d100_priv")]
|
||||
#[test_case(Rule::UndocumentedPublicModule, Path::new("_unrelated/_no_pkg_priv.py"); "no_d100_priv_script")]
|
||||
#[test_case(Rule::UndocumentedPublicNestedClass, Path::new("D.py"); "D106")]
|
||||
#[test_case(Rule::UndocumentedPublicPackage, Path::new("D.py"); "D104_0")]
|
||||
#[test_case(Rule::UndocumentedPublicPackage, Path::new("D104/__init__.py"); "D104_1")]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
source: crates/ruff/src/rules/pydocstyle/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
[]
|
||||
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
---
|
||||
source: crates/ruff/src/rules/pydocstyle/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
- kind:
|
||||
name: UndocumentedPublicModule
|
||||
body: Missing docstring in public module
|
||||
suggestion: ~
|
||||
fixable: false
|
||||
location:
|
||||
row: 1
|
||||
column: 0
|
||||
end_location:
|
||||
row: 1
|
||||
column: 0
|
||||
fix:
|
||||
edits: []
|
||||
parent: ~
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
source: crates/ruff/src/rules/pydocstyle/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
[]
|
||||
|
||||
|
|
@ -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…
Reference in New Issue