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

import lost after formatting #1542

Closed
arthurxlee opened this issue Oct 8, 2020 · 4 comments · Fixed by #1553, hypothesis/viahtml#107 or wwade/jobrunner#63
Closed

import lost after formatting #1542

arthurxlee opened this issue Oct 8, 2020 · 4 comments · Fixed by #1553, hypothesis/viahtml#107 or wwade/jobrunner#63
Labels
bug Something isn't working

Comments

@arthurxlee
Copy link

I have a file that looks like this

from xxxxxxxxxxxxxxxx import AAAAAAAAAA, BBBBBBBBBB
from xxxxxxxxxxxxxxxx import CCCCCCCCC, DDDDDDDDD  # xxxxxxxxxxxxxxxxxx

print(CCCCCCCCC)

The content from line 2 could be folded to line1, but I suspect the comment at the end of line 2 gets in the way and the entire line will be lost after formatting.

Using isort version 5.5.4

@timothycrosley
Copy link
Member

timothycrosley commented Oct 8, 2020

@arthurxlee, can you provide the output of isort . --show-config?
isort should never lose imports (unless they are duplicated) running this code snippet with vanilla settings gives me:

from xxxxxxxxxxxxxxxx import (  # xxxxxxxxxxxxxxxxxx
    AAAAAAAAAA,
    BBBBBBBBBB,
    CCCCCCCCC,
    DDDDDDDDD,
)

print(CCCCCCCCC)

Which, I believe is correct. This leads me to believe it is happening only with certain settings, or that the actual code is different from the given example in some way.

In case it's helpful, this UI: https://pycqa.github.io/isort/docs/quick_start/0.-try/ can help to produce a minimal example.

Thanks!

~Timothy

@timothycrosley timothycrosley added the repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue. label Oct 8, 2020
@arthurxlee
Copy link
Author

arthurxlee commented Oct 9, 2020

Attached is the output from show config.
isort_config.txt

@arthurxlee
Copy link
Author

I would also suggest that the lines should not be folded since the inline comment would very probably not apply to the other lines, so them folding may not be desirable.

@timothycrosley
Copy link
Member

timothycrosley commented Oct 10, 2020

Thank you for sharing your configuration, and I'm sorry you've been impacted by this issue! With the configuration, I was able to quickly confirm the issue was isolated to the wrap mode being used (which is a relatively newer community contributed mode). I've fixed this issue and added additional regression tests to ensure it doesn't happen going forward.

I would also suggest that the lines should not be folded since the inline comment would very probably not apply to the other lines, so them folding may not be desirable.

I agree with this in principle, though it is really very hard to tell comment intention.
For instance this slightly modified example where B should go to line 1 and C should go to line 2.

from xxxxxxxxxxxxxxxx import AAAAAAAAAA, CCCCCCCCC
from xxxxxxxxxxxxxxxx import BBBBBBBBB, DDDDDDDDD  # Comment for B

In theory there is some optimizations that could be made for just this wrap-mode, but if you really want to make sure comment intention is explicitly mapped, the only way to do that would be to use a mode that leads to one import per a line. Otherwise, for individual cases like this you can tell isort to skip the line with the comment:

from xxxxxxxxxxxxxxxx import AAAAAAAAAA, CCCCCCCCC
from xxxxxxxxxxxxxxxx import BBBBBBBBB, DDDDDDDDD  # Comment for D isort: skip

Thanks!

~Timothy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment