Skip to content

Commit 85f7871

Browse files
authored
[flake8-builtins] Default to non-strict checking (A005) (#16125)
## Summary This PR changes the default value of `lint.flake8-builtins.builtins-strict-checking` added in #15951 from `true` to `false`. This also allows simplifying the default option logic and removes the dependence on preview mode. #15399 was already closed by #15951, but this change will finalize the behavior mentioned in #15399 (comment). As an example, strict checking flags modules based on their last component, so `utils/logging.py` triggers A005. Non-strict checking checks the path to the module, so `utils/logging.py` is allowed (this is the example and desired behavior from #15399 exactly) but a top-level `logging.py` or `logging/__init__.py` is still disallowed. ## Test Plan Existing tests from #15951 and #16006, with the snapshot updated in `a005_module_shadowing_strict_default` to reflect the new default.
1 parent daeb8f1 commit 85f7871

File tree

8 files changed

+23
-46
lines changed

8 files changed

+23
-46
lines changed

crates/ruff/tests/lint.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,11 +2556,7 @@ fn a005_module_shadowing_strict_default() -> Result<()> {
25562556
----- stdout -----
25572557
abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
25582558
collections/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
2559-
collections/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
2560-
foobar/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
2561-
foobar/collections/__init__.py:1:1: A005 Module `collections` shadows a Python standard-library module
2562-
foobar/collections/abc/__init__.py:1:1: A005 Module `abc` shadows a Python standard-library module
2563-
Found 6 errors.
2559+
Found 2 errors.
25642560
25652561
----- stderr -----
25662562
");

crates/ruff/tests/snapshots/show_settings__display_default_settings.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ linter.flake8_bandit.allowed_markup_calls = []
231231
linter.flake8_bugbear.extend_immutable_calls = []
232232
linter.flake8_builtins.allowed_modules = []
233233
linter.flake8_builtins.ignorelist = []
234-
linter.flake8_builtins.strict_checking = true
234+
linter.flake8_builtins.strict_checking = false
235235
linter.flake8_comprehensions.allow_dict_calls_with_keyword_arguments = false
236236
linter.flake8_copyright.notice_rgx = (?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*
237237
linter.flake8_copyright.author = none

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ use crate::settings::LinterSettings;
2323
/// Standard-library modules can be marked as exceptions to this rule via the
2424
/// [`lint.flake8-builtins.allowed-modules`] configuration option.
2525
///
26-
/// By default, only the last component of the module name is considered, so `logging.py`,
27-
/// `utils/logging.py`, and `utils/logging/__init__.py` would all clash with the builtin `logging`
28-
/// module. With the [`lint.flake8-builtins.strict-checking`] option set to `false`, the module
29-
/// path is considered, so only a top-level `logging.py` or `logging/__init__.py` will trigger the
30-
/// rule and `utils/logging.py`, for example, would not. In preview mode, the default value of
31-
/// [`lint.flake8-builtins.strict-checking`] is `false` rather than `true` in stable mode.
26+
/// By default, the module path relative to the project root or [`src`] directories is considered,
27+
/// so a top-level `logging.py` or `logging/__init__.py` will clash with the builtin `logging`
28+
/// module, but `utils/logging.py`, for example, will not. With the
29+
/// [`lint.flake8-builtins.builtins-strict-checking`] option set to `true`, only the last component
30+
/// of the module name is considered, so `logging.py`, `utils/logging.py`, and
31+
/// `utils/logging/__init__.py` will all trigger the rule.
3232
///
3333
/// This rule is not applied to stub files, as the name of a stub module is out
3434
/// of the control of the author of the stub file. Instead, a stub should aim to

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Settings for the `flake8-builtins` plugin.
22
3-
use crate::{display_settings, settings::types::PreviewMode};
3+
use crate::display_settings;
44
use ruff_macros::CacheKey;
55
use std::fmt::{Display, Formatter};
66

@@ -11,16 +11,6 @@ pub struct Settings {
1111
pub strict_checking: bool,
1212
}
1313

14-
impl Settings {
15-
pub fn new(preview: PreviewMode) -> Self {
16-
Self {
17-
ignorelist: Vec::new(),
18-
allowed_modules: Vec::new(),
19-
strict_checking: preview.is_disabled(),
20-
}
21-
}
22-
}
23-
2414
impl Display for Settings {
2515
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2616
display_settings! {

crates/ruff_workspace/src/configuration.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,6 @@ impl Configuration {
235235

236236
let rules = lint.as_rule_table(lint_preview)?;
237237

238-
let flake8_builtins = lint
239-
.flake8_builtins
240-
.map(|builtins| builtins.into_settings(lint_preview))
241-
.unwrap_or_else(|| {
242-
ruff_linter::rules::flake8_builtins::settings::Settings::new(lint_preview)
243-
});
244-
245238
// LinterSettings validation
246239
let isort = lint
247240
.isort
@@ -344,7 +337,10 @@ impl Configuration {
344337
.flake8_bugbear
345338
.map(Flake8BugbearOptions::into_settings)
346339
.unwrap_or_default(),
347-
flake8_builtins,
340+
flake8_builtins: lint
341+
.flake8_builtins
342+
.map(Flake8BuiltinsOptions::into_settings)
343+
.unwrap_or_default(),
348344
flake8_comprehensions: lint
349345
.flake8_comprehensions
350346
.map(Flake8ComprehensionsOptions::into_settings)

crates/ruff_workspace/src/options.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use ruff_linter::rules::{
2828
pycodestyle, pydoclint, pydocstyle, pyflakes, pylint, pyupgrade, ruff,
2929
};
3030
use ruff_linter::settings::types::{
31-
IdentifierPattern, OutputFormat, PreviewMode, PythonVersion, RequiredVersion,
31+
IdentifierPattern, OutputFormat, PythonVersion, RequiredVersion,
3232
};
3333
use ruff_linter::{warn_user_once, RuleSelector};
3434
use ruff_macros::{CombineOptions, OptionsMetadata};
@@ -1267,9 +1267,9 @@ pub struct Flake8BuiltinsOptions {
12671267
///
12681268
/// This option is ignored if both `strict-checking` and `builtins-strict-checking` are set.
12691269
#[option(
1270-
default = r#"true"#,
1270+
default = r#"false"#,
12711271
value_type = "bool",
1272-
example = "builtins-strict-checking = false"
1272+
example = "builtins-strict-checking = true"
12731273
)]
12741274
#[deprecated(
12751275
since = "0.10.0",
@@ -1280,21 +1280,16 @@ pub struct Flake8BuiltinsOptions {
12801280
/// Compare module names instead of full module paths.
12811281
///
12821282
/// Used by [`A005` - `stdlib-module-shadowing`](https://docs.astral.sh/ruff/rules/stdlib-module-shadowing/).
1283-
///
1284-
/// In preview mode the default value is `false` rather than `true`.
12851283
#[option(
1286-
default = r#"true"#,
1284+
default = r#"false"#,
12871285
value_type = "bool",
1288-
example = "strict-checking = false"
1286+
example = "strict-checking = true"
12891287
)]
12901288
pub strict_checking: Option<bool>,
12911289
}
12921290

12931291
impl Flake8BuiltinsOptions {
1294-
pub fn into_settings(
1295-
self,
1296-
preview: PreviewMode,
1297-
) -> ruff_linter::rules::flake8_builtins::settings::Settings {
1292+
pub fn into_settings(self) -> ruff_linter::rules::flake8_builtins::settings::Settings {
12981293
#[allow(deprecated)]
12991294
ruff_linter::rules::flake8_builtins::settings::Settings {
13001295
ignorelist: self
@@ -1309,7 +1304,7 @@ impl Flake8BuiltinsOptions {
13091304
.strict_checking
13101305
.or(self.builtins_strict_checking)
13111306
// use the old default of true on non-preview
1312-
.unwrap_or(preview.is_disabled()),
1307+
.unwrap_or_default(),
13131308
}
13141309
}
13151310
}

crates/ruff_workspace/src/pyproject.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ mod tests {
210210

211211
use ruff_linter::codes;
212212
use ruff_linter::line_width::LineLength;
213-
use ruff_linter::settings::types::{PatternPrefixPair, PreviewMode};
213+
use ruff_linter::settings::types::PatternPrefixPair;
214214

215215
use crate::options::{Flake8BuiltinsOptions, LintCommonOptions, LintOptions, Options};
216216
use crate::pyproject::{find_settings_toml, parse_pyproject_toml, Pyproject, Tools};
@@ -363,7 +363,7 @@ strict-checking = false
363363
})
364364
);
365365

366-
let settings = expected.into_settings(PreviewMode::Enabled);
366+
let settings = expected.into_settings();
367367

368368
assert_eq!(settings.allowed_modules, vec!["sys".to_string()]);
369369
assert_eq!(

ruff.schema.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)