From 9a27b37a918e9d33ae489230352774311789f604 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 8 Jan 2025 12:38:34 +0000 Subject: [PATCH] [`flake8-builtins`] Rename `A005` and improve its error message (#15348) --- crates/ruff_linter/src/checkers/filesystem.rs | 6 ++--- crates/ruff_linter/src/codes.rs | 2 +- crates/ruff_linter/src/registry.rs | 2 +- .../src/rules/flake8_builtins/mod.rs | 24 +++++++++--------- .../src/rules/flake8_builtins/rules/mod.rs | 4 +-- ...hadowing.rs => stdlib_module_shadowing.rs} | 25 ++++++++++--------- ...5_A005__modules__logging____init__.py.snap | 3 +-- ...005_A005__modules__package__bisect.py.snap | 3 +-- ...e__bisect.py_builtins_allowed_modules.snap | 3 +-- ...__A005_A005__modules__package__xml.py.snap | 3 +-- ...05_A005__modules__string____init__.py.snap | 3 +-- ...___init__.py_builtins_allowed_modules.snap | 3 +-- 12 files changed, 38 insertions(+), 43 deletions(-) rename crates/ruff_linter/src/rules/flake8_builtins/rules/{builtin_module_shadowing.rs => stdlib_module_shadowing.rs} (70%) diff --git a/crates/ruff_linter/src/checkers/filesystem.rs b/crates/ruff_linter/src/checkers/filesystem.rs index 15ba2ed5f6c27..b9d2ecb3f4549 100644 --- a/crates/ruff_linter/src/checkers/filesystem.rs +++ b/crates/ruff_linter/src/checkers/filesystem.rs @@ -5,7 +5,7 @@ use ruff_python_trivia::CommentRanges; use crate::package::PackageRoot; use crate::registry::Rule; -use crate::rules::flake8_builtins::rules::builtin_module_shadowing; +use crate::rules::flake8_builtins::rules::stdlib_module_shadowing; use crate::rules::flake8_no_pep420::rules::implicit_namespace_package; use crate::rules::pep8_naming::rules::invalid_module_name; use crate::settings::LinterSettings; @@ -45,8 +45,8 @@ pub(crate) fn check_file_path( } // flake8-builtins - if settings.rules.enabled(Rule::BuiltinModuleShadowing) { - if let Some(diagnostic) = builtin_module_shadowing( + if settings.rules.enabled(Rule::StdlibModuleShadowing) { + if let Some(diagnostic) = stdlib_module_shadowing( path, package, &settings.flake8_builtins.builtins_allowed_modules, diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index f3f626c9a7a8d..fd5942d893bfa 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -317,7 +317,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Flake8Builtins, "002") => (RuleGroup::Stable, rules::flake8_builtins::rules::BuiltinArgumentShadowing), (Flake8Builtins, "003") => (RuleGroup::Stable, rules::flake8_builtins::rules::BuiltinAttributeShadowing), (Flake8Builtins, "004") => (RuleGroup::Stable, rules::flake8_builtins::rules::BuiltinImportShadowing), - (Flake8Builtins, "005") => (RuleGroup::Preview, rules::flake8_builtins::rules::BuiltinModuleShadowing), + (Flake8Builtins, "005") => (RuleGroup::Preview, rules::flake8_builtins::rules::StdlibModuleShadowing), (Flake8Builtins, "006") => (RuleGroup::Preview, rules::flake8_builtins::rules::BuiltinLambdaArgumentShadowing), // flake8-bugbear diff --git a/crates/ruff_linter/src/registry.rs b/crates/ruff_linter/src/registry.rs index 2ec1c86fd0708..7cd4eaa257907 100644 --- a/crates/ruff_linter/src/registry.rs +++ b/crates/ruff_linter/src/registry.rs @@ -306,7 +306,7 @@ impl Rule { Rule::UnsortedImports | Rule::MissingRequiredImport => LintSource::Imports, Rule::ImplicitNamespacePackage | Rule::InvalidModuleName - | Rule::BuiltinModuleShadowing => LintSource::Filesystem, + | Rule::StdlibModuleShadowing => LintSource::Filesystem, Rule::IndentationWithInvalidMultiple | Rule::IndentationWithInvalidMultipleComment | Rule::MissingWhitespace diff --git a/crates/ruff_linter/src/rules/flake8_builtins/mod.rs b/crates/ruff_linter/src/rules/flake8_builtins/mod.rs index 3c32ef0185a27..03dfd77d28ba5 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_builtins/mod.rs @@ -21,26 +21,26 @@ mod tests { #[test_case(Rule::BuiltinAttributeShadowing, Path::new("A003.py"))] #[test_case(Rule::BuiltinImportShadowing, Path::new("A004.py"))] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/non_builtin/__init__.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/logging/__init__.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/string/__init__.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/package/bisect.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/_abc/__init__.py") )] - #[test_case(Rule::BuiltinModuleShadowing, Path::new("A005/modules/package/xml.py"))] + #[test_case(Rule::StdlibModuleShadowing, Path::new("A005/modules/package/xml.py"))] #[test_case(Rule::BuiltinLambdaArgumentShadowing, Path::new("A006.py"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy()); @@ -80,26 +80,26 @@ mod tests { } #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/non_builtin/__init__.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/logging/__init__.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/string/__init__.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/package/bisect.py") )] #[test_case( - Rule::BuiltinModuleShadowing, + Rule::StdlibModuleShadowing, Path::new("A005/modules/_abc/__init__.py") )] - #[test_case(Rule::BuiltinModuleShadowing, Path::new("A005/modules/package/xml.py"))] + #[test_case(Rule::StdlibModuleShadowing, Path::new("A005/modules/package/xml.py"))] fn builtins_allowed_modules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!( "{}_{}_builtins_allowed_modules", diff --git a/crates/ruff_linter/src/rules/flake8_builtins/rules/mod.rs b/crates/ruff_linter/src/rules/flake8_builtins/rules/mod.rs index 46478f7c236a1..1f9aeeb12ece9 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/rules/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_builtins/rules/mod.rs @@ -2,12 +2,12 @@ pub(crate) use builtin_argument_shadowing::*; pub(crate) use builtin_attribute_shadowing::*; pub(crate) use builtin_import_shadowing::*; pub(crate) use builtin_lambda_argument_shadowing::*; -pub(crate) use builtin_module_shadowing::*; pub(crate) use builtin_variable_shadowing::*; +pub(crate) use stdlib_module_shadowing::*; mod builtin_argument_shadowing; mod builtin_attribute_shadowing; mod builtin_import_shadowing; mod builtin_lambda_argument_shadowing; -mod builtin_module_shadowing; mod builtin_variable_shadowing; +mod stdlib_module_shadowing; diff --git a/crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_module_shadowing.rs b/crates/ruff_linter/src/rules/flake8_builtins/rules/stdlib_module_shadowing.rs similarity index 70% rename from crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_module_shadowing.rs rename to crates/ruff_linter/src/rules/flake8_builtins/rules/stdlib_module_shadowing.rs index 385cf2cf1ed7b..486ceb14a1afc 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_module_shadowing.rs +++ b/crates/ruff_linter/src/rules/flake8_builtins/rules/stdlib_module_shadowing.rs @@ -11,34 +11,35 @@ use crate::package::PackageRoot; use crate::settings::types::PythonVersion; /// ## What it does -/// Checks for modules that use the same names as Python builtin modules. +/// Checks for modules that use the same names as Python standard-library +/// modules. /// /// ## Why is this bad? -/// Reusing a builtin module name for the name of a module increases the -/// difficulty of reading and maintaining the code, and can cause -/// non-obvious errors, as readers may mistake the variable for the -/// builtin and vice versa. +/// Reusing a standard-library module name for the name of a module increases +/// the difficulty of reading and maintaining the code, and can cause +/// non-obvious errors. Readers may mistake the first-party module for the +/// standard-library module and vice versa. /// -/// Builtin modules can be marked as exceptions to this rule via the +/// Standard-library modules can be marked as exceptions to this rule via the /// [`lint.flake8-builtins.builtins-allowed-modules`] configuration option. /// /// ## Options /// - `lint.flake8-builtins.builtins-allowed-modules` #[derive(ViolationMetadata)] -pub(crate) struct BuiltinModuleShadowing { +pub(crate) struct StdlibModuleShadowing { name: String, } -impl Violation for BuiltinModuleShadowing { +impl Violation for StdlibModuleShadowing { #[derive_message_formats] fn message(&self) -> String { - let BuiltinModuleShadowing { name } = self; - format!("Module `{name}` is shadowing a Python builtin module") + let StdlibModuleShadowing { name } = self; + format!("Module `{name}` shadows a Python standard-library module") } } /// A005 -pub(crate) fn builtin_module_shadowing( +pub(crate) fn stdlib_module_shadowing( path: &Path, package: Option>, allowed_modules: &[String], @@ -74,7 +75,7 @@ pub(crate) fn builtin_module_shadowing( } Some(Diagnostic::new( - BuiltinModuleShadowing { + StdlibModuleShadowing { name: module_name.to_string(), }, TextRange::default(), diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__logging____init__.py.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__logging____init__.py.snap index 068a24e8f22b9..1e4858a65e0fa 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__logging____init__.py.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__logging____init__.py.snap @@ -1,5 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs -snapshot_kind: text --- -__init__.py:1:1: A005 Module `logging` is shadowing a Python builtin module +__init__.py:1:1: A005 Module `logging` shadows a Python standard-library module diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py.snap index 967e38ec66575..573d2f7e85c13 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py.snap @@ -1,5 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs -snapshot_kind: text --- -bisect.py:1:1: A005 Module `bisect` is shadowing a Python builtin module +bisect.py:1:1: A005 Module `bisect` shadows a Python standard-library module diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py_builtins_allowed_modules.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py_builtins_allowed_modules.snap index 967e38ec66575..573d2f7e85c13 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py_builtins_allowed_modules.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__bisect.py_builtins_allowed_modules.snap @@ -1,5 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs -snapshot_kind: text --- -bisect.py:1:1: A005 Module `bisect` is shadowing a Python builtin module +bisect.py:1:1: A005 Module `bisect` shadows a Python standard-library module diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__xml.py.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__xml.py.snap index 4d7c0f2231525..9e70b02ef5a9d 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__xml.py.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__package__xml.py.snap @@ -1,5 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs -snapshot_kind: text --- -xml.py:1:1: A005 Module `xml` is shadowing a Python builtin module +xml.py:1:1: A005 Module `xml` shadows a Python standard-library module diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py.snap index cec55e06e8bc5..1bfc390c9e06f 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py.snap @@ -1,5 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs -snapshot_kind: text --- -__init__.py:1:1: A005 Module `string` is shadowing a Python builtin module +__init__.py:1:1: A005 Module `string` shadows a Python standard-library module diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py_builtins_allowed_modules.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py_builtins_allowed_modules.snap index cec55e06e8bc5..1bfc390c9e06f 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py_builtins_allowed_modules.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A005_A005__modules__string____init__.py_builtins_allowed_modules.snap @@ -1,5 +1,4 @@ --- source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs -snapshot_kind: text --- -__init__.py:1:1: A005 Module `string` is shadowing a Python builtin module +__init__.py:1:1: A005 Module `string` shadows a Python standard-library module