Sorting of imports (isort) is different if the file has unsaved modifications
See original GitHub issueEnvironment data
- VS Code version: 1.32.3 (have also seen this on older versions)
- Extension version: 2019.2.5558 (have also seen this on older versions)
- OS and version: Ubuntu 16.04.6
- Python version: 3.5.2 (have also seen this on other versions)
- Type of virtual environment used: venv
- Relevant/affected Python packages and their versions: I currently have isort 4.3.15, though I’ve seen this on older versions
Expected behaviour
That when running the “Sort Imports” command on a file, the imports are sorted according to my setup.cfg
config regardless of whether the file is currently saved or not.
Actual behaviour
When running the “Sort Imports” command on a file:
- If the file is saved, my
setup.cfg
config is respected - If the file has modifications, my
setup.cfg
config is not respected
Steps to reproduce:
Given this config:
# setup.cfg
[isort]
length_sort = True
# demo.py
import os
import logging
import collections
- put
setup.cfg
anddemo.py
next to each other in a new folder - open that folder as a workspace in VSCode
- open
demo.py
- “Sort imports” within
demo.py
, it should be unchanged - edit
demo.py
, without saving it - “Sort imports” within
demo.py
, note that its imports have changed ordering to ignore thelength_sort
option
This reproduces with much more complex configs, and other options are also ignored, so I strongly suspect the setup.cfg
file isn’t being picked up as a whole if the file is modified.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:10
Top Results From Across the Web
Configuration options for isort
Sort imports by their string length. Type: Bool Default: False Config default: false. Python & Config File Name: length_sort. CLI Flags:.
Read more >VS Code group-aware sort imports on save for TypeScript 4.7
Any ideas what the difference is? Could the on-save organize not be using 4.7, or is this a different organization method? visual-studio-code ...
Read more >Delete and restore files
Once you are in the Deleted files tab, you can view, sort, and filter any deleted files that you had access to. Here...
Read more >Use isort to sort your Python module imports automatically
Besides, the --check option of isort will only check the formatting of the modules and won't change them in place. The isort command...
Read more >Fix List for IBM Rational DOORS Requirements ...
If you have modified this file, please merge the modifications. ... In the "Manage document" window, enable to sort the document list and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe that I’m experiencing the same issue that Peter originally brought up.
In particular, here’s how the issue arises:
Upon saving, my imports are re-ordered. The configurations in my
settings.cfg
file seems to be ignored.If I were to commit the changes to git at this point, the diff would be a bit unwieldy, even thought the actual contents of the file were not changed before I saved.
Then, without making any changes to the file, I save it again. This completely reorders the imports to what they previously were. The configurations in my
settings.cfg
file are used.So, in summary…
settings.cfg
into accountsettings.cfg
into account.My current workaround
To save, I have to press
cmd + s
twice in a row each time I save a file to ensure that my imports end up in the right order.The effect on my work
It’s definitely annoying, but it’s not an earth-melting issue. I’m still able to get work done with VS Code. That said, because saving a file is such a frequent action, this annoyance comes up a lot.
If anyone wants a workaround, setting the location of your settings file (pyproject.toml/setup.cfg) via an argument to isort works well.