Skip to content

Commit 6185a2a

Browse files
authored
[pyupgrade] Fix false positive on relative imports from local .builtins module (UP029) (#21309)
1 parent 6cc3393 commit 6185a2a

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .builtins import next
2+
from ..builtins import str
3+
from ...builtins import int
4+
from .builtins import next as _next
5+

crates/ruff_linter/src/checkers/ast/analyze/statement.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
717717
}
718718
if checker.is_rule_enabled(Rule::UnnecessaryBuiltinImport) {
719719
if let Some(module) = module {
720-
pyupgrade::rules::unnecessary_builtin_import(checker, stmt, module, names);
720+
pyupgrade::rules::unnecessary_builtin_import(
721+
checker, stmt, module, names, level,
722+
);
721723
}
722724
}
723725
if checker.any_rule_enabled(&[

crates/ruff_linter/src/rules/pyupgrade/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ mod tests {
9999
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_many_empty_lines.py"))]
100100
#[test_case(Rule::UnicodeKindPrefix, Path::new("UP025.py"))]
101101
#[test_case(Rule::UnnecessaryBuiltinImport, Path::new("UP029_0.py"))]
102+
#[test_case(Rule::UnnecessaryBuiltinImport, Path::new("UP029_2.py"))]
102103
#[test_case(Rule::UnnecessaryClassParentheses, Path::new("UP039.py"))]
103104
#[test_case(Rule::UnnecessaryDefaultTypeArgs, Path::new("UP043.py"))]
104105
#[test_case(Rule::UnnecessaryEncodeUTF8, Path::new("UP012.py"))]

crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_builtin_import.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ pub(crate) fn unnecessary_builtin_import(
7575
stmt: &Stmt,
7676
module: &str,
7777
names: &[Alias],
78+
level: u32,
7879
) {
80+
// Ignore relative imports (they're importing from local modules, not Python's builtins).
81+
if level > 0 {
82+
return;
83+
}
84+
7985
// Ignore irrelevant modules.
8086
if !matches!(
8187
module,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
3+
---
4+

0 commit comments

Comments
 (0)