Skip to content

Commit 42d25ca

Browse files
committed
fix PLC2701 false positive for dunder submodules and improve diagnostic ranges
1 parent 7e408a5 commit 42d25ca

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ pub(crate) fn import_private_name(checker: &Checker, scope: &Scope) {
116116
.qualified_name
117117
.segments()
118118
.iter()
119-
.find_position(|name| name.starts_with('_'))
119+
.find_position(|name| {
120+
name.starts_with('_') && !(name.starts_with("__") && name.ends_with("__"))
121+
})
120122
else {
121123
continue;
122124
};
@@ -137,7 +139,25 @@ pub(crate) fn import_private_name(checker: &Checker, scope: &Scope) {
137139
} else {
138140
None
139141
};
140-
checker.report_diagnostic(ImportPrivateName { name, module }, binding.range());
142+
let range = match binding.source.map(|id| checker.semantic().statement(id)) {
143+
Some(ruff_python_ast::Stmt::ImportFrom(import_from)) => {
144+
if index < import_info.module_name.len() {
145+
import_from
146+
.module
147+
.as_ref()
148+
.map_or(binding.range(), |m| m.range())
149+
} else {
150+
import_from
151+
.names
152+
.iter()
153+
.find(|alias| alias.name.as_str() == import_info.member_name)
154+
.map_or(binding.range(), |alias| alias.range())
155+
}
156+
}
157+
_ => binding.range(),
158+
};
159+
160+
checker.report_diagnostic(ImportPrivateName { name, module }, range);
141161
}
142162
}
143163

crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name__submodule____main__.py.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
source: crates/ruff_linter/src/rules/pylint/mod.rs
33
---
44
PLC2701 Private name import `_a`
5-
--> __main__.py:2:16
5+
--> __main__.py:2:6
66
|
77
1 | # Errors.
88
2 | from _a import b
9-
| ^
9+
| ^^
1010
3 | from c._d import e
1111
4 | from _f.g import h
1212
|
1313

1414
PLC2701 Private name import `_d` from external module `c`
15-
--> __main__.py:3:18
15+
--> __main__.py:3:6
1616
|
1717
1 | # Errors.
1818
2 | from _a import b
1919
3 | from c._d import e
20-
| ^
20+
| ^^^^
2121
4 | from _f.g import h
2222
5 | from i import _j
2323
|
2424

2525
PLC2701 Private name import `_f`
26-
--> __main__.py:4:18
26+
--> __main__.py:4:6
2727
|
2828
2 | from _a import b
2929
3 | from c._d import e
3030
4 | from _f.g import h
31-
| ^
31+
| ^^^^
3232
5 | from i import _j
3333
6 | from k import _l as m
3434
|
@@ -45,12 +45,12 @@ PLC2701 Private name import `_j` from external module `i`
4545
|
4646

4747
PLC2701 Private name import `_l` from external module `k`
48-
--> __main__.py:6:21
48+
--> __main__.py:6:15
4949
|
5050
4 | from _f.g import h
5151
5 | from i import _j
5252
6 | from k import _l as m
53-
| ^
53+
| ^^^^^^^
5454
7 | import _aaa
5555
8 | import bbb.ccc._ddd as eee # Panicked in https://github.com/astral-sh/ruff/pull/5920
5656
|

0 commit comments

Comments
 (0)