-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[isort
] Fix syntax error after docstring ending with backslash (I002
)
#19505
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
[isort
] Fix syntax error after docstring ending with backslash (I002
)
#19505
Conversation
…row before inserting required imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
#[test_case(Path::new("whitespace.py"))] | |
fn required_import(path: &Path) -> Result<()> { | |
let snapshot = format!("required_import_{}", path.to_string_lossy()); |
|
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? |
@@ -794,6 +794,7 @@ mod tests { | |||
#[test_case(Path::new("comments_and_newlines.py"))] | |||
#[test_case(Path::new("docstring.py"))] | |||
#[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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine, but good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new snapshots look great, thank you! This is good to merge once CI passes.
@@ -794,6 +794,7 @@ mod tests { | |||
#[test_case(Path::new("comments_and_newlines.py"))] | |||
#[test_case(Path::new("docstring.py"))] | |||
#[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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.toml
file: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: