When checking module visibility, don't check entire ancestry (#3835)

This commit is contained in:
Nazia Povey 2023-04-03 11:38:41 -04:00 committed by GitHub
parent d2f2544f6e
commit 849091d846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 50 additions and 19 deletions

View 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")]

View File

@ -0,0 +1,6 @@
---
source: crates/ruff/src/rules/pydocstyle/mod.rs
expression: diagnostics
---
[]

View File

@ -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: ~

View File

@ -0,0 +1,6 @@
---
source: crates/ruff/src/rules/pydocstyle/mod.rs
expression: diagnostics
---
[]

View File

@ -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,

View File

@ -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
}