-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
diagnostic rendering for import lint I001
is sub-optimal in some cases
#15504
Comments
Edit: Ignore this, see comment below This is confusing because if I add a debug print to display the line number, it seems to give the right answer for the range emitted from the lint: > cargo run -p ruff -- check tmp.py --no-cache --preview --isolated --select I001
[crates/ruff_linter/src/rules/isort/rules/organize_imports.rs:96:5] &range = 0..176
[crates/ruff_linter/src/rules/isort/rules/organize_imports.rs:97:5] locator.compute_source_location(range.end()) = SourceLocation {
row: 9,
column: 37,
}
tmp.py:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from __future__ import annotations
2 | |
3 | | from typing import Any
4 | |
5 | | from requests import Session
6 | |
7 | | from my_first_party import my_first_party_object
8 | |
9 | | from . import my_local_folder_object
10 | |
11 | |
12 | |
| |__^ I001
13 | class Foo:...
|
= help: Organize imports
Found 1 error.
[*] 1 fixable with the `--fix` option. Is there some transformation done to the range before it gets to the diagnostic/user? |
|
ah, actually my mistake - I was printing the range from the "early return" diagnostic. there's additional logic in the lint that makes a different range, which explicitly includes trailing whitespace: ruff/crates/ruff_linter/src/rules/isort/rules/organize_imports.rs Lines 135 to 136 in c034e28
It looks like this choice is related to implementing the fix. |
On current
main
, here's what one example of aI001
diagnostic looks like:In particular, the above comes from this snapshot:
https://github.com/astral-sh/ruff/blob/55a7f72035390cf1093e4da0cf2462e793bfbce1/crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__lines_after_imports.pyi.snap
In the above rendering, the
^
is pointing to a class definition, but it should be pointing to an import statement. After #15359 is merged, the rendering will improve somewhat to this:This is better, but still not ideal. However, this isn't a problem in the diagnostic rendering itself (which is what #15359 didn't address it), but rather, in the spans generated by the
I001
lint. To demonstrate, this is a minimal reproducer I made by debug printing theannotate-snippets::Message
when running the above test:This outputs the same rendering as above. And the span,
0..180
, corresponds to this substring:So whatever is generating the spans here is including those empty lines at the end. We should fix the span generation to be tighter. (It's perhaps as simple as trimming the trailing lines from the range.)
The text was updated successfully, but these errors were encountered: