mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
[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
5 changed files with 50 additions and 21 deletions
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,
|
||||
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<()> {
|
||||
|
@ -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!(
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
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;
|
||||
|
@ -9,6 +7,9 @@ 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.
|
||||
///
|
||||
|
@ -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?;
|
||||
|
||||
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(),
|
||||
));
|
||||
}
|
||||
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;
|
||||
}
|
||||
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…
Add table
Add a link
Reference in a new issue