[pylint] Allow dunder submodules and improve diagnostic range (PLC2701)#22804
[pylint] Allow dunder submodules and improve diagnostic range (PLC2701)#22804ntBre merged 4 commits intoastral-sh:mainfrom
pylint] Allow dunder submodules and improve diagnostic range (PLC2701)#22804Conversation
42d25ca to
8312a9a
Compare
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PLC2701 | 1965 | 978 | 987 | 0 | 0 |
ntBre
left a comment
There was a problem hiding this comment.
Thanks for working on this! I had one small nit about the code, and then some suggestions for refining the diagnostic range a bit more.
crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs
Outdated
Show resolved
Hide resolved
.../ruff_linter__rules__pylint__tests__PLC2701_import_private_name__submodule____main__.py.snap
Outdated
Show resolved
Hide resolved
.../ruff_linter__rules__pylint__tests__PLC2701_import_private_name__submodule____main__.py.snap
Outdated
Show resolved
Hide resolved
8312a9a to
436cad2
Compare
ntBre
left a comment
There was a problem hiding this comment.
Thanks! This is looking good. I pushed a commit simplifying the implementation slightly, but then I thought of a very tricky edge case where simply summing the lengths of the qualified name would fail. I think we should try to locate the next token instead. See my inline comment for one example I found in another rule.
crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs
Outdated
Show resolved
Hide resolved
b6f5b5a to
d284772
Compare
pylint] Allow dunder submodules and improve diagnostic range (PLC2701)
Summary
This PR addresses two improvements for the
PLC2701(import-private-name) rule:__main__was being flagged as a private name import. It now ensures that any segment identified as a "dunder" (starting and ending with__) is ignored by the rule.from pkg._private import func) or the imported member (e.g.,from pkg import _private).Closes #22187
Test Plan
repro.pyscript covering three scenarios:from pkg import _private(Surgical highlight on member)from pkg._private import func(Surgical highlight on module path)from pkg.__main__ import func(Correctly ignored)crates/ruff_linter/src/rules/pylint/snapshots/. The diffs confirm that diagnostic ranges have shifted from the imported name to the actual private name causing the violation.