[isort] Fix syntax error after docstring ending with backslash (I002)#19505
Conversation
…row before inserting required imports
ntBre
left a comment
There was a problem hiding this comment.
This is great, thank you!
Would you mind adding an I002 test too? That would also have the benefit of showing a nice snapshot instead of comparing to a TextSize directly. I think 22 is correct from counting manually, but the snapshot will be an easy way to double check :)
I think you could probably add another test_case on this function or one of the others nearby:
ruff/crates/ruff_linter/src/rules/isort/mod.rs
Lines 804 to 806 in 1f0d39a
|
|
Don't worry about the ecosystem check yet, I think it's relative to an old commit since I only just approved the workflow. I expect it to clear up after a new commit. |
|
Thanks for your reply! I've added a snapshot test in my latest commit, how does it look? |
| #[test_case(Path::new("docstring.pyi"))] | ||
| #[test_case(Path::new("docstring_followed_by_continuation.py"))] | ||
| #[test_case(Path::new("docstring_only.py"))] | ||
| #[test_case(Path::new("docstring_with_continuation.py"))] |
There was a problem hiding this comment.
@ntBre Do you think it would be a good idea to rename this existing test case (docstring_with_continuation.py) to something like docstring_with_semicolon_and_continuation.py to avoid ambiguity with the test case that I just added (i.e. docstring_followed_by_continuation.py)?
There was a problem hiding this comment.
I think it's fine, but good catch!
ntBre
left a comment
There was a problem hiding this comment.
The new snapshots look great, thank you! This is good to merge once CI passes.
| #[test_case(Path::new("docstring.pyi"))] | ||
| #[test_case(Path::new("docstring_followed_by_continuation.py"))] | ||
| #[test_case(Path::new("docstring_only.py"))] | ||
| #[test_case(Path::new("docstring_with_continuation.py"))] |
There was a problem hiding this comment.
I think it's fine, but good catch!
isort] Fix syntax error after docstring ending with backslash (I002)
Issue: #19498
Summary
missing-required-import inserts the missing import on the line immediately following the last line of the docstring. However, if the dosctring is immediately followed by a continuation token (i.e. backslash) then this leads to a syntax error because Python interprets the docstring and the inserted import to be on the same line.
The proposed solution in this PR is to check if the first token after a file docstring is a continuation character, and if so, to advance an additional line before inserting the missing import.
Test Plan
Added a unit test, and the following example was verified manually:
Given this simple test Python file:
and this ruff linting configuration in the
pyproject.tomlfile:Without the changes in this PR, the ruff linter would try to insert the missing import in line 2, resulting in a syntax error, and report the following:
error: Fix introduced a syntax error. Reverting all changes.With the changes in this PR, ruff correctly advances one more line before adding the missing import, resulting in the following output: