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

Float To Top errors when file only has newlines #1453

Closed
johnpaulett opened this issue Sep 3, 2020 · 2 comments · Fixed by #1457
Closed

Float To Top errors when file only has newlines #1453

johnpaulett opened this issue Sep 3, 2020 · 2 comments · Fixed by #1457
Labels
bug Something isn't working

Comments

@johnpaulett
Copy link

I had a blank __init__.py with a newline in it, with float_to_top = true in my .isort.cfg after upgrading from 4.x.

The __init__.py should not have the newline, but I had removed a few imports and left the newline in the file. Unfortunately, the isort error made tracking down which file was causing the issue difficult, since I was running isort . on a large project.

Encountered with 0.5.2, 0.5.4, & 0.5.5.

A minimal example:

$ echo "\n" > example.py
$ .venv/bin/isort --float-to-top example.py
Traceback (most recent call last):
  File ".venv/bin/isort", line 10, in <module>
    sys.exit(main())
  File ".venv/lib/python3.8/site-packages/isort/main.py", line 876, in main
    for sort_attempt in attempt_iterator:
  File ".venv/lib/python3.8/site-packages/isort/main.py", line 865, in <genexpr>
    sort_imports(  # type: ignore
  File ".venv/lib/python3.8/site-packages/isort/main.py", line 93, in sort_imports
    incorrectly_sorted = not api.sort_file(
  File ".venv/lib/python3.8/site-packages/isort/api.py", line 321, in sort_file
    changed = sort_stream(
  File ".venv/lib/python3.8/site-packages/isort/api.py", line 158, in sort_stream
    changed = core.process(
  File ".venv/lib/python3.8/site-packages/isort/core.py", line 89, in process
    while current[-1] == "\n":
IndexError: string index out of range

Removing the blank line in example.py resolves the issue. Also not an issue with --float-to-top not used.

Ideally this file would not cause any issue when --float-to-top is used. It may also be nice if an indication is given as to what file is being processed when internal errors occur, to allow manual review of that file for Python syntax errors.

@timothycrosley timothycrosley added the bug Something isn't working label Sep 4, 2020
timothycrosley added a commit that referenced this issue Sep 4, 2020
…sure errors return back file name when they occur.
timothycrosley added a commit that referenced this issue Sep 4, 2020
…th-empty-file

Resolve #1453: isort error when float to top on almost empty file. En…
@johnpaulett
Copy link
Author

Wonderful! Thanks @timothycrosley

@timothycrosley
Copy link
Member

No problem happy I could help! Just released it to PyPI as 5.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants