[flake8-builtins] Make strict module name comparison optional (A005) (#15951)

## Summary

This PR adds the configuration option
`lint.flake8-builtins.builtins-strict-checking`, which is used in A005
to determine whether the fully-qualified module name (relative to the
project root or source directories) should be checked instead of just
the final component as is currently the case.

As discussed in
https://github.com/astral-sh/ruff/issues/15399#issuecomment-2587017147,
the default value of the new option is `false` on preview, so modules
like `utils.logging` from the initial report are no longer flagged by
default. For non-preview the default is still strict checking.

## Test Plan

New A005 test module with the structure reported in #15399.

Fixes #15399
This commit is contained in:
Brent Westbrook 2025-02-09 19:33:03 -05:00 committed by GitHub
parent f367aa8367
commit 88b543d73a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 175 additions and 76 deletions

View file

@ -12,6 +12,7 @@ mod tests {
use crate::assert_messages;
use crate::registry::Rule;
use crate::rules::flake8_builtins;
use crate::settings::types::PythonVersion;
use crate::settings::LinterSettings;
use crate::test::{test_path, test_resource_path};
@ -50,7 +51,13 @@ mod tests {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("flake8_builtins").join(path).as_path(),
&LinterSettings::for_rule(rule_code),
&LinterSettings {
flake8_builtins: flake8_builtins::settings::Settings {
builtins_strict_checking: true,
..Default::default()
},
..LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
@ -74,7 +81,13 @@ mod tests {
);
let diagnostics = test_path(
Path::new("flake8_builtins").join(path).as_path(),
&LinterSettings::for_rule(rule_code),
&LinterSettings {
flake8_builtins: flake8_builtins::settings::Settings {
builtins_strict_checking: strict,
..Default::default()
},
..LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
@ -92,6 +105,10 @@ mod tests {
Path::new("flake8_builtins").join(path).as_path(),
&LinterSettings {
src: vec![test_resource_path(src.join(path.parent().unwrap()))],
flake8_builtins: flake8_builtins::settings::Settings {
builtins_strict_checking: false,
..Default::default()
},
..LinterSettings::for_rule(rule_code)
},
)?;
@ -112,6 +129,10 @@ mod tests {
Path::new("flake8_builtins").join(path).as_path(),
&LinterSettings {
project_root: test_resource_path(src.join(path.parent().unwrap())),
flake8_builtins: flake8_builtins::settings::Settings {
builtins_strict_checking: false,
..Default::default()
},
..LinterSettings::for_rule(rule_code)
},
)?;
@ -179,6 +200,7 @@ mod tests {
&LinterSettings {
flake8_builtins: super::settings::Settings {
builtins_allowed_modules: vec!["xml".to_string(), "logging".to_string()],
builtins_strict_checking: true,
..Default::default()
},
..LinterSettings::for_rules(vec![rule_code])

View file

@ -93,6 +93,11 @@ pub(crate) fn stdlib_module_shadowing(
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(),

View file

@ -1,6 +1,6 @@
//! Settings for the `flake8-builtins` plugin.
use crate::display_settings;
use crate::{display_settings, settings::types::PreviewMode};
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};
@ -8,6 +8,17 @@ use std::fmt::{Display, Formatter};
pub struct Settings {
pub builtins_ignorelist: Vec<String>,
pub builtins_allowed_modules: Vec<String>,
pub builtins_strict_checking: bool,
}
impl Settings {
pub fn new(preview: PreviewMode) -> Self {
Self {
builtins_ignorelist: Vec::new(),
builtins_allowed_modules: Vec::new(),
builtins_strict_checking: preview.is_disabled(),
}
}
}
impl Display for Settings {
@ -18,6 +29,7 @@ impl Display for Settings {
fields = [
self.builtins_allowed_modules | array,
self.builtins_ignorelist | array,
self.builtins_strict_checking,
]
}
Ok(())

View file

@ -1,4 +1,4 @@
---
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
---
logging.py:1:1: A005 Module `logging` shadows a Python standard-library module