Skip to content

Commit

Permalink
first/last approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Feb 6, 2025
1 parent 6e628b1 commit e2768ce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
16 changes: 6 additions & 10 deletions crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,13 +2300,11 @@ 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
collections/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
foobar/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
foobar/collections/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
foobar/collections/abc/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
foobar/collections/foobar/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
Found 8 errors.
foobar/collections/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
Found 6 errors.
----- stderr -----
");
Expand Down Expand Up @@ -2342,13 +2340,11 @@ 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
collections/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
foobar/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
foobar/collections/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
foobar/collections/abc/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
foobar/collections/foobar/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
Found 8 errors.
foobar/collections/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
Found 6 errors.
----- stderr -----
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,31 @@ pub(crate) fn stdlib_module_shadowing(
Cow::from(path.with_extension(""))
};

// convert a filesystem path like `foobar/collections/abc` to a sequence of modules like
// `["foobar", "collections", "abc"]`, stripping anything that's not a normal component
for component in path
// 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
let mut components = path
.components()
.filter(|c| matches!(c, Component::Normal(_)))
.map(|c| c.as_os_str().to_string_lossy())
{
if !is_allowed_module(settings, &component) {
return Some(Diagnostic::new(
StdlibModuleShadowing {
name: component.to_string(),
},
TextRange::default(),
));
}
// in non-strict mode we only consider the first component
if !settings.flake8_builtins.builtins_strict_checking {
break;
}
.map(|c| c.as_os_str().to_string_lossy());

// 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()?
};

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

None
Some(Diagnostic::new(
StdlibModuleShadowing {
name: module_name.to_string(),
},
TextRange::default(),
))
}

/// Return the longest prefix of `path` between `settings.src` and `settings.project_root`.
Expand Down

0 comments on commit e2768ce

Please sign in to comment.