Skip to content

Commit

Permalink
[flake8-builtins] Exempt private built-in modules (A005) (#14505)
Browse files Browse the repository at this point in the history
## Summary

Resolves #12949.

## Test Plan

`cargo nextest run` and `cargo insta test`.
  • Loading branch information
InSyncWithFoo authored Nov 24, 2024
1 parent e3d7926 commit 545e9de
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
Empty file.
8 changes: 8 additions & 0 deletions crates/ruff_linter/src/rules/flake8_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ mod tests {
Rule::BuiltinModuleShadowing,
Path::new("A005/modules/package/bisect.py")
)]
#[test_case(
Rule::BuiltinModuleShadowing,
Path::new("A005/modules/_abc/__init__.py")
)]
#[test_case(Rule::BuiltinModuleShadowing, Path::new("A005/modules/package/xml.py"))]
#[test_case(Rule::BuiltinLambdaArgumentShadowing, Path::new("A006.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
Expand Down Expand Up @@ -91,6 +95,10 @@ mod tests {
Rule::BuiltinModuleShadowing,
Path::new("A005/modules/package/bisect.py")
)]
#[test_case(
Rule::BuiltinModuleShadowing,
Path::new("A005/modules/_abc/__init__.py")
)]
#[test_case(Rule::BuiltinModuleShadowing, Path::new("A005/modules/package/xml.py"))]
fn builtins_allowed_modules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::path::Path;

use crate::package::PackageRoot;
use crate::settings::types::PythonVersion;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::PySourceType;
use ruff_python_stdlib::path::is_module_file;
use ruff_python_stdlib::sys::is_known_standard_library;
use ruff_text_size::TextRange;

use crate::package::PackageRoot;
use crate::settings::types::PythonVersion;

/// ## What it does
/// Checks for modules that use the same names as Python builtin modules.
///
Expand Down Expand Up @@ -47,25 +48,35 @@ pub(crate) fn builtin_module_shadowing(
return None;
}

if let Some(package) = package {
let module_name = if is_module_file(path) {
package.path().file_name().unwrap().to_string_lossy()
} else {
path.file_stem().unwrap().to_string_lossy()
};
let package = package?;

let module_name = if is_module_file(path) {
package.path().file_name().unwrap().to_string_lossy()
} else {
path.file_stem().unwrap().to_string_lossy()
};

if !is_known_standard_library(target_version.minor(), &module_name) {
return None;
}

// Shadowing private stdlib modules is okay.
// https://github.com/astral-sh/ruff/issues/12949
if module_name.starts_with('_') && !module_name.starts_with("__") {
return None;
}

if is_known_standard_library(target_version.minor(), &module_name)
&& allowed_modules
.iter()
.all(|allowed_module| allowed_module != &module_name)
{
return Some(Diagnostic::new(
BuiltinModuleShadowing {
name: module_name.to_string(),
},
TextRange::default(),
));
}
if allowed_modules
.iter()
.any(|allowed_module| allowed_module == &module_name)
{
return None;
}
None

Some(Diagnostic::new(
BuiltinModuleShadowing {
name: module_name.to_string(),
},
TextRange::default(),
))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
snapshot_kind: text
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
snapshot_kind: text
---

0 comments on commit 545e9de

Please sign in to comment.