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

Found a dangerous edge case: Comment converted to code #1949

Open
phil20686 opened this issue Jul 5, 2022 · 3 comments
Open

Found a dangerous edge case: Comment converted to code #1949

phil20686 opened this issue Jul 5, 2022 · 3 comments
Assignees

Comments

@phil20686
Copy link

running isort over the following file:

from math import sqrt

if __name__ == "__main__":
    thing = sqrt(3)
#   some random comments
    from datetime import datetime
    dt = datetime(2020,1,1)

Leads to the comment being converted to code

from math import sqrt

if __name__ == "__main__":
    thing = sqrt(3)
some random comments
    from datetime import datetime
    dt = datetime(2020,1,1)

will be the output. The isort version is 5.10.1. I have not checked all isort versions.

@mawillcockson
Copy link

I minimized the original example a bit:

import sys

if True:
#   comment
    import sys

I used this script with git bisect, which pointed to 67b2a54, which is now:

isort/isort/core.py

Lines 415 to 418 in 12cc5fb

if indent:
import_section = "".join(
line[len(indent) :] for line in import_section.splitlines(keepends=True)
)

@anirudnits
Copy link
Collaborator

This is similar to #2087.

@anirudnits
Copy link
Collaborator

I'm trying to come up with a solution for this, I added comments on why this is happening and the difficulties I'm facing while resolving them.

@anirudnits anirudnits self-assigned this Mar 18, 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

3 participants