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 uncomments code when odd indentation is next to an import #2087

Open
pfranz opened this issue Feb 7, 2023 · 2 comments · May be fixed by #2109
Open

isort uncomments code when odd indentation is next to an import #2087

pfranz opened this issue Feb 7, 2023 · 2 comments · May be fixed by #2109
Assignees

Comments

@pfranz
Copy link

pfranz commented Feb 7, 2023

I came across a bug where isort strips off the leading characters (in my case a comment) when there's indentation next to an import. I found this in an older version of isort, but still see it in 5.12.0

#!/usr/bin/env python3

if True:
# this comment breaks
    import sys

when invoked with isort break_isort.py results in

#!/usr/bin/env python3

if True:
is comment breaks
    import sys
@anirudnits anirudnits self-assigned this Mar 18, 2023
@anirudnits
Copy link
Collaborator

anirudnits commented Mar 18, 2023

The value for the indentation level of the comment gets set here: https://github.com/PyCQA/isort/blob/main/isort/core.py#L340, basically whats this is doing is setting the indentation level of the whole block without imports to the new indentation of the import block.

@anirudnits
Copy link
Collaborator

I'm thinking of the various use-cases that arise there, one simple fix here is to the convert the last conditional in line#338 to

...
and ( not did_contain_imports and len(new_indent) < len(indent) )
```

that would solve this issue but this my interfere with certain cimports.

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

Successfully merging a pull request may close this issue.

2 participants