Skip to content

[flake8-builtins] Default to non-strict checking (A005) #16125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2556,11 +2556,7 @@ fn a005_module_shadowing_strict_default() -> 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 `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 `abc` shadows a Python standard-library module
Found 6 errors.
Found 2 errors.

----- stderr -----
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ linter.flake8_bandit.allowed_markup_calls = []
linter.flake8_bugbear.extend_immutable_calls = []
linter.flake8_builtins.allowed_modules = []
linter.flake8_builtins.ignorelist = []
linter.flake8_builtins.strict_checking = true
linter.flake8_builtins.strict_checking = false
linter.flake8_comprehensions.allow_dict_calls_with_keyword_arguments = false
linter.flake8_copyright.notice_rgx = (?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*
linter.flake8_copyright.author = none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use crate::settings::LinterSettings;
/// Standard-library modules can be marked as exceptions to this rule via the
/// [`lint.flake8-builtins.allowed-modules`] configuration option.
///
/// By default, only the last component of the module name is considered, so `logging.py`,
/// `utils/logging.py`, and `utils/logging/__init__.py` would all clash with the builtin `logging`
/// module. With the [`lint.flake8-builtins.strict-checking`] option set to `false`, the module
/// path is considered, so only a top-level `logging.py` or `logging/__init__.py` will trigger the
/// rule and `utils/logging.py`, for example, would not. In preview mode, the default value of
/// [`lint.flake8-builtins.strict-checking`] is `false` rather than `true` in stable mode.
/// By default, the module path relative to the project root or [`src`] directories is considered,
/// so a top-level `logging.py` or `logging/__init__.py` will clash with the builtin `logging`
/// module, but `utils/logging.py`, for example, will not. With the
/// [`lint.flake8-builtins.builtins-strict-checking`] option set to `true`, only the last component
/// of the module name is considered, so `logging.py`, `utils/logging.py`, and
/// `utils/logging/__init__.py` will all trigger the rule.
///
/// This rule is not applied to stub files, as the name of a stub module is out
/// of the control of the author of the stub file. Instead, a stub should aim to
Expand Down
12 changes: 1 addition & 11 deletions crates/ruff_linter/src/rules/flake8_builtins/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Settings for the `flake8-builtins` plugin.

use crate::{display_settings, settings::types::PreviewMode};
use crate::display_settings;
use ruff_macros::CacheKey;
use std::fmt::{Display, Formatter};

Expand All @@ -11,16 +11,6 @@ pub struct Settings {
pub strict_checking: bool,
}

impl Settings {
pub fn new(preview: PreviewMode) -> Self {
Self {
ignorelist: Vec::new(),
allowed_modules: Vec::new(),
strict_checking: preview.is_disabled(),
}
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
display_settings! {
Expand Down
12 changes: 4 additions & 8 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,6 @@ impl Configuration {

let rules = lint.as_rule_table(lint_preview)?;

let flake8_builtins = lint
.flake8_builtins
.map(|builtins| builtins.into_settings(lint_preview))
.unwrap_or_else(|| {
ruff_linter::rules::flake8_builtins::settings::Settings::new(lint_preview)
});

// LinterSettings validation
let isort = lint
.isort
Expand Down Expand Up @@ -344,7 +337,10 @@ impl Configuration {
.flake8_bugbear
.map(Flake8BugbearOptions::into_settings)
.unwrap_or_default(),
flake8_builtins,
flake8_builtins: lint
.flake8_builtins
.map(Flake8BuiltinsOptions::into_settings)
.unwrap_or_default(),
flake8_comprehensions: lint
.flake8_comprehensions
.map(Flake8ComprehensionsOptions::into_settings)
Expand Down
19 changes: 7 additions & 12 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ruff_linter::rules::{
pycodestyle, pydoclint, pydocstyle, pyflakes, pylint, pyupgrade, ruff,
};
use ruff_linter::settings::types::{
IdentifierPattern, OutputFormat, PreviewMode, PythonVersion, RequiredVersion,
IdentifierPattern, OutputFormat, PythonVersion, RequiredVersion,
};
use ruff_linter::{warn_user_once, RuleSelector};
use ruff_macros::{CombineOptions, OptionsMetadata};
Expand Down Expand Up @@ -1267,9 +1267,9 @@ pub struct Flake8BuiltinsOptions {
///
/// This option is ignored if both `strict-checking` and `builtins-strict-checking` are set.
#[option(
default = r#"true"#,
default = r#"false"#,
value_type = "bool",
example = "builtins-strict-checking = false"
example = "builtins-strict-checking = true"
)]
#[deprecated(
since = "0.10.0",
Expand All @@ -1280,21 +1280,16 @@ pub struct Flake8BuiltinsOptions {
/// Compare module names instead of full module paths.
///
/// Used by [`A005` - `stdlib-module-shadowing`](https://docs.astral.sh/ruff/rules/stdlib-module-shadowing/).
///
/// In preview mode the default value is `false` rather than `true`.
#[option(
default = r#"true"#,
default = r#"false"#,
value_type = "bool",
example = "strict-checking = false"
example = "strict-checking = true"
)]
pub strict_checking: Option<bool>,
}

impl Flake8BuiltinsOptions {
pub fn into_settings(
self,
preview: PreviewMode,
) -> ruff_linter::rules::flake8_builtins::settings::Settings {
pub fn into_settings(self) -> ruff_linter::rules::flake8_builtins::settings::Settings {
#[allow(deprecated)]
ruff_linter::rules::flake8_builtins::settings::Settings {
ignorelist: self
Expand All @@ -1309,7 +1304,7 @@ impl Flake8BuiltinsOptions {
.strict_checking
.or(self.builtins_strict_checking)
// use the old default of true on non-preview
.unwrap_or(preview.is_disabled()),
.unwrap_or_default(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ mod tests {

use ruff_linter::codes;
use ruff_linter::line_width::LineLength;
use ruff_linter::settings::types::{PatternPrefixPair, PreviewMode};
use ruff_linter::settings::types::PatternPrefixPair;

use crate::options::{Flake8BuiltinsOptions, LintCommonOptions, LintOptions, Options};
use crate::pyproject::{find_settings_toml, parse_pyproject_toml, Pyproject, Tools};
Expand Down Expand Up @@ -363,7 +363,7 @@ strict-checking = false
})
);

let settings = expected.into_settings(PreviewMode::Enabled);
let settings = expected.into_settings();

assert_eq!(settings.allowed_modules, vec!["sys".to_string()]);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading