Skip to content
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

I002 doesn't handle comments with newlines properly #6504

Closed
durumu opened this issue Aug 11, 2023 · 0 comments
Closed

I002 doesn't handle comments with newlines properly #6504

durumu opened this issue Aug 11, 2023 · 0 comments

Comments

@durumu
Copy link
Contributor

durumu commented Aug 11, 2023

Hello! I noticed a case where ruff has different behavior than isort, which I think is a bug.

Here is an example Python file with comments that have newlines, and no docstring:

# This is a comment

# This is another comment

from sys import stdout

stdout.write("Hello world\n")

When I run ruff --select I002 --fix test.py, with the following .ruff.toml:

[isort]
required-imports = ["from __future__ import annotations"]

I get this result:

# This is a comment
from __future__ import annotations

# This is another comment

from sys import stdout

stdout.write("Hello world\n")

However, when I run isort -a "from __future__ import annotations" on this file, I get this result (which I think is more correct):

# This is a comment

# This is another comment

from __future__ import annotations

from sys import stdout

stdout.write("Hello world\n")

Edit: I was able to reproduce this using the latest version of ruff, 0.0.284.

charliermarsh pushed a commit that referenced this issue Aug 12, 2023
## Summary

When adding an import, such as when fixing `I002`, ruff doesn't skip
whitespace between comments, but isort does. See this issue for more
detail: #6504

This change would fix that by skipping whitespace between comments in
`Insertion.start_of_file()`.

## Test Plan

I added a new test, `comments_and_newlines`, to verify this behavior. I
also ran `cargo test` and no existing tests broke. That being said, this
is technically a breaking change, as it's possible that someone was
relying on the previous behavior.
@durumu durumu closed this as completed Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant