isort version: 5.2.2
python version: 3.7.7
OS: macOS Catalina
Imports separated by code are always sorted to the top of the file. For example, this
import os
def my_func():
pass
import sys
sorts to this
import os
import sys
def my_func():
pass
This was the behavior in isort 4.x.x.
Imports separated by code are no longer sorted to the top of the file, even if there are no special comments present that would normally prevent such behavior (i.e. #
isort: skip, # isort: split).
Issues #468, #693, and #706 discussed changing the expected behavior for sorting, but my understanding is that this was supposed to be an opt-in change.
The expected behavior of always sorting is actually very useful not only for organization but also for developer focus. For example, consider writing a new function in the middle of a very long file (long enough that top of the file is not in view). If a new import needs to be added, this is normally accomplished by moving to the top of the file, adding the import, and then moving back to the original location. These steps can be slightly distracting because they require shifting one's focus away from the code under development. A better way to handle this is to install isort and set up one's code editor to auto sort on save (vscode has this nicely integrated). In this case, adding the import can be done _in the middle_ of the function (properly shifted to the very left, of course). On save, isort will automatically move the import to the top and sort it with the other imports. Very convenient!
@claytonlemons,
I also love the ability to avoid context switching when writing code, allowing isort to move my imports to the top for me. Don't worry isort 5 didn't remove this feature! It is behind a flag, simply because it is inherently less safe and does utilize more resources then a purely streaming approach. The new flag is --float-to-top from the CLI or float_to_top from a config file. I've added a section to the upgrade doc to go over this https://timothycrosley.github.io/isort/docs/upgrade_guides/5.0.0/#imports-no-longer-moved-to-top.
Thanks!
~Timothy
I'm seeing the same behaviour, and that flag doesn't seem to be working. I'm
using the same example as in the OP:
isort version : VERSION 5.1.4
python version : Python 3.7.6
mac os : 10.14.6
Before:
-> % cat isort_test.py
import os
def my_func():
pass
import sys
After:
-> % isort isort_test.py --float-to-top
(neovim) use@comp [17:46:16] [~/.config/nvim] [master *]
-> % cat isort_test.py
import os
def my_func():
pass
import sys
Hi @geo7,
There's a bug that keeps it from taking effect unless isort would make a change to any of the individual sections. This is fixed in develop and will be deployed soon. You can test to see if this is the cause for your exact issue by adding an empty comment (which isort will remove) to the second import:
import os
def my_func():
pass
import sys #
Thanks!
~Timothy
@timothycrosley Thanks for pointing out the new flag and migration documentation! It worked like a charm for my case.
I think this issue can be closed, unless you had some additional action items related to it.
I kept it open to capture the failure case with the new flag that @geo7 mentioned, that is now fixed as well in the 5.3.0 release, so closing this now.
Thanks!
~Timothy