mirror of https://github.com/astral-sh/ruff
[`flake8-builtins`] Exempt private built-in modules (`A005`) (#14505)
## Summary Resolves #12949. ## Test Plan `cargo nextest run` and `cargo insta test`.
This commit is contained in:
parent
e3d792605f
commit
545e9deba3
0
crates/ruff_linter/resources/test/fixtures/flake8_builtins/A005/modules/_abc/__init__.py
vendored
Normal file
0
crates/ruff_linter/resources/test/fixtures/flake8_builtins/A005/modules/_abc/__init__.py
vendored
Normal file
|
|
@ -36,6 +36,10 @@ mod tests {
|
||||||
Rule::BuiltinModuleShadowing,
|
Rule::BuiltinModuleShadowing,
|
||||||
Path::new("A005/modules/package/bisect.py")
|
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::BuiltinModuleShadowing, Path::new("A005/modules/package/xml.py"))]
|
||||||
#[test_case(Rule::BuiltinLambdaArgumentShadowing, Path::new("A006.py"))]
|
#[test_case(Rule::BuiltinLambdaArgumentShadowing, Path::new("A006.py"))]
|
||||||
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
|
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||||
|
|
@ -91,6 +95,10 @@ mod tests {
|
||||||
Rule::BuiltinModuleShadowing,
|
Rule::BuiltinModuleShadowing,
|
||||||
Path::new("A005/modules/package/bisect.py")
|
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::BuiltinModuleShadowing, Path::new("A005/modules/package/xml.py"))]
|
||||||
fn builtins_allowed_modules(rule_code: Rule, path: &Path) -> Result<()> {
|
fn builtins_allowed_modules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||||
let snapshot = format!(
|
let snapshot = format!(
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::package::PackageRoot;
|
|
||||||
use crate::settings::types::PythonVersion;
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::PySourceType;
|
use ruff_python_ast::PySourceType;
|
||||||
|
|
@ -9,6 +7,9 @@ use ruff_python_stdlib::path::is_module_file;
|
||||||
use ruff_python_stdlib::sys::is_known_standard_library;
|
use ruff_python_stdlib::sys::is_known_standard_library;
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
|
|
||||||
|
use crate::package::PackageRoot;
|
||||||
|
use crate::settings::types::PythonVersion;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## 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 builtin modules.
|
||||||
///
|
///
|
||||||
|
|
@ -47,25 +48,35 @@ pub(crate) fn builtin_module_shadowing(
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(package) = package {
|
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)
|
let module_name = if is_module_file(path) {
|
||||||
&& allowed_modules
|
package.path().file_name().unwrap().to_string_lossy()
|
||||||
.iter()
|
} else {
|
||||||
.all(|allowed_module| allowed_module != &module_name)
|
path.file_stem().unwrap().to_string_lossy()
|
||||||
{
|
};
|
||||||
return Some(Diagnostic::new(
|
|
||||||
BuiltinModuleShadowing {
|
if !is_known_standard_library(target_version.minor(), &module_name) {
|
||||||
name: module_name.to_string(),
|
return None;
|
||||||
},
|
|
||||||
TextRange::default(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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 allowed_modules
|
||||||
|
.iter()
|
||||||
|
.any(|allowed_module| allowed_module == &module_name)
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Diagnostic::new(
|
||||||
|
BuiltinModuleShadowing {
|
||||||
|
name: module_name.to_string(),
|
||||||
|
},
|
||||||
|
TextRange::default(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
|
||||||
|
snapshot_kind: text
|
||||||
|
---
|
||||||
|
|
||||||
Loading…
Reference in New Issue