Skip to content

Commit b96ab9f

Browse files
committed
parent approach
1 parent e2768ce commit b96ab9f

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

crates/ruff/tests/lint.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,9 +2321,7 @@ fn a005_module_shadowing_strict() -> Result<()> {
23212321
----- stdout -----
23222322
abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
23232323
collections/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
2324-
collections/abc/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
2325-
collections/foobar/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
2326-
Found 4 errors.
2324+
Found 2 errors.
23272325
23282326
----- stderr -----
23292327
");

crates/ruff_linter/src/rules/flake8_builtins/rules/stdlib_module_shadowing.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ pub(crate) fn stdlib_module_shadowing(
8080
};
8181

8282
// convert a filesystem path like `foobar/collections/abc` to a reversed sequence of modules
83-
// like `["foobar", "collections", "abc"]`, stripping anything that's not a normal component
83+
// like `["abc", "collections", "foobar"]`, stripping anything that's not a normal component
8484
let mut components = path
8585
.components()
8686
.filter(|c| matches!(c, Component::Normal(_)))
87-
.map(|c| c.as_os_str().to_string_lossy());
87+
.map(|c| c.as_os_str().to_string_lossy())
88+
.rev();
8889

89-
// in strict mode, check the last path component, matching the upstream rule. in non-strict
90-
// mode, only compare the first component (the root package)
91-
let module_name = if settings.flake8_builtins.builtins_strict_checking {
92-
components.last()?
93-
} else {
94-
components.next()?
95-
};
90+
let module_name = components.next()?;
9691

9792
if is_allowed_module(settings, &module_name) {
9893
return None;
9994
}
10095

96+
// not allowed generally, but check for a parent in non-strict mode
97+
if !settings.flake8_builtins.builtins_strict_checking && components.next().is_some() {
98+
return None;
99+
}
100+
101101
Some(Diagnostic::new(
102102
StdlibModuleShadowing {
103103
name: module_name.to_string(),

0 commit comments

Comments
 (0)