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

isort copies comments across imports #2282

Open
nicolasstucki opened this issue Jul 23, 2024 · 4 comments
Open

isort copies comments across imports #2282

nicolasstucki opened this issue Jul 23, 2024 · 4 comments

Comments

@nicolasstucki
Copy link

nicolasstucki commented Jul 23, 2024

Given this file,

from foo import a, b  # comment
from foo import c as d
from foo import e

the result of isort is

from foo import a, b  # comment
from foo import c as d
from foo import e  # comment
@nicolasstucki
Copy link
Author

This is particularly problematic with # pyright: ignore comments as it starts duplicating them. In general, this might break any tool that uses comments semantically in import lines.

@Helveg
Copy link

Helveg commented Jul 29, 2024

You could use a temporary workaround by enabling the combine_as_imports configuration and would give you this output:

from foo import a, b, c as d, e  # comment

if that works for you?

@Helveg
Copy link

Helveg commented Jul 29, 2024

The problem seems to be that isort categorizes the from foo import a, b # comment as a from-type comment belonging to the entire foo module, and then considers it a comment for all from foo import statements (as-imports are treated differently). The solution is to have the comment categorized as a nested-type comment for the foo.a and foo.b members. isort does this properly for the 4th statement in this snippet:

from foo import a, b
from foo import c as d
from foo import e
from foo import f # comment-f

whose output shows that for nested comments there's no duplication:

from foo import f # comment-f
from foo import a, b
from foo import c as d
from foo import e

@nicolasstucki
Copy link
Author

nicolasstucki commented Aug 8, 2024

I used this workaround

from foo import a # comment
from foo import b # comment
from foo import c as d
from foo import e

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

2 participants