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 deletes lines with a typo #1688

Closed
bersbersbers opened this issue Mar 9, 2021 · 2 comments · Fixed by #1692
Closed

isort deletes lines with a typo #1688

bersbersbers opened this issue Mar 9, 2021 · 2 comments · Fixed by #1692
Labels
enhancement New feature or request

Comments

@bersbersbers
Copy link

isort 5.7.0 invoked with

printf "from package1 import alright\nfrom package2 imprt and_its_gone\nfrom package3 import also_ok" | isort -

outputs

from package1 import alright
from package3 import also_ok

The second line is missing.

@gofr
Copy link
Contributor

gofr commented Mar 11, 2021

What would you like isort to do in this case?

I think this probably happens with all lines that start with one of these strings and then later turn out to be invalid import statements:

IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*") + CIMPORT_IDENTIFIERS

I can think of many ways to handle this. For example fix the typo, treat the line as an import and sort it somehow anyway, or treat the line as a non-import and move it down. But I think the simplest and most useful thing here is to use the atomic option, so isort exits with a syntax error.

@bersbersbers
Copy link
Author

bersbersbers commented Mar 11, 2021

Thank you - --atomic is somewhat helpful in my use case (VS Code), although it triggers a notification that is not auto-dismissed when the error is corrected. That is something for VS Code to consider, I guess.

For the other question, consider VS Code with default settings as a use case (https://code.visualstudio.com/docs/python/editing). Unless I am mistaken, it auto-sorts on each save, so a typo should not be uncommon for isort to see. Clearly, the line should not be deleted, as I have just typed it. On the other hand, sorting the line someplace else (out of view, potentially) is not ideal, either, although there's still a linter to detect that.

I think for the sake of consistency, you could do the same thing you do when you hit a line that is not detected as an import at all (frm package import something) - these currently stay in place and sometimes are separated from the rest by an extra line break, which I think is a good solution:

printf "from package1 import alright\nfrm package2 import and_its_gone\nfrom package3 import also_ok" | isort -:

from package1 import alright

frm package2 import and_its_gone
from package3 import also_ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants