Skip to content

Commit

Permalink
parent approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Feb 6, 2025
1 parent e2768ce commit b96ab9f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
4 changes: 1 addition & 3 deletions crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2321,9 +2321,7 @@ fn a005_module_shadowing_strict() -> Result<()> {
----- stdout -----
abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
collections/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
collections/abc/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
collections/foobar/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
Found 4 errors.
Found 2 errors.
----- stderr -----
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,24 @@ pub(crate) fn stdlib_module_shadowing(
};

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

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

if is_allowed_module(settings, &module_name) {
return None;
}

// not allowed generally, but check for a parent in non-strict mode
if !settings.flake8_builtins.builtins_strict_checking && components.next().is_some() {
return None;
}

Some(Diagnostic::new(
StdlibModuleShadowing {
name: module_name.to_string(),
Expand Down

0 comments on commit b96ab9f

Please sign in to comment.