How to make isort black compatible. Original Question: isort conflicts with black?
tordbb opened this issue ยท 21 comments
Hi.
I experience that black and isort undo eachothers changes when working on some of my files.
I am using the following two first steps in my .pre-commit-config.yaml
:
repos:
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.5.4
hooks:
- id: isort
When I run pre-commit run --all-files
, both black and isort report they are making changes.
The changes result to the following formatting in my file configs.py
:
from datetime import date
from cost_categories import (applsj, asjdfsi, bananana, sdjffsd, sjdifjsl,
sjdil, yoyoyoyoy)
from library_user import User
However, if I remove the isort
-hook from the yaml file, the conflict stops.
Then, I get the following output (as dictated by black alone):
from datetime import date
from cost_categories import (
applsj,
asjdfsi,
bananana,
sdjffsd,
sjdifjsl,
sjdil,
yoyoyoyoy,
)
from library_user import User
How should I approach this? Am I using some wrong revision?
you can choose the black
profile in isort
that should solve the conflict. You could include that in the .isort.cfg
file in your repository.
@anirudnits is correct. The easiest way to do this would be to create a .isort.cfg file at the root of your repository with the following:
[tool.isort]
profile = "black"
Alternatively, you could update your precommit to set the profile when running isort:
- repo: https://github.com/pycqa/isort
rev: 5.5.4
hooks:
- id: isort
args: ["--profile", "black"]
See: https://pycqa.github.io/isort/docs/configuration/config_files/
and: https://pycqa.github.io/isort/docs/configuration/profiles/
Hope this is helpful! Let us know if we were able to resolve your issue :).
Thanks!
~Timothy
Thanks, this solution worked beautifully!
- repo: https://github.com/pycqa/isort
rev: 5.5.4
hooks:
- id: isort
args: ["--profile", "black"]
Glad to hear! This has come up a couple of times so I'm going to pin this issue until the documentation makes this solution more prominent so others that run into it can also see how to fix compatibility
I was just experiencing a similar issue.
Black wanted to split up an import statement, and then isort wanted to undo blacks changes.
I solved it by adding a --line-length parameter also in the arguments for isort. The resulting .pre-commit-config.yaml looks like this:
repos:
- repo: https://github.com/psf/black
rev: 21.6b0
hooks:
- id: black
args: [--line-length=72]
- repo: https://github.com/pycqa/isort
rev: 5.9.1
hooks:
- id: isort
args: ["--profile", "black", --line-length=72]
Line length is a nice hackish way of solving this but it creates a new problem, I don't want line-length to be 72.
Can we reopen this issue?
@timothycrosley
@alexisgaziello couldn't you just set it something other than 72? Whatever you're preferred line length is? Am I missing something?
The .isort.cfg does not seem to work for me I keep getting an error saying to files will be fixed. I also tried to add multi_line_output:
[tool.isort]
profile = "black"
multi_line_output = 3
I've run into this error adding black and isort pre-commit configuration to the pipenv
project tonight, and unfortunately the isort profile of black seems to have no effect, tried both the .isort.cfg
and adding it to the arguments of the pre commit .yml.
Ah my bad, I was doing it wrong somehow. Got it working now with and .isort.cfg
that has this. The example I followed above was for a toml file.
[settings]
profile = black
, --line-length=72]
This was what solved it for me as well! Thank you!
Ah my bad, I was doing it wrong somehow. Got it working now with and
.isort.cfg
that has this. The example I followed above was for a toml file.[settings] profile = black
thanks for posting this, helped ๐
I can't seem to get this to work. I tried all the above methods except for pre-commit hooks because I don't use them. I am in VSCode and formatting on save. I tried in pyproject.toml
, .isort.cfg
, and in the settings JSON of VSCode with combinations of profile
, line_length
, and multi_line_output
.
It does not appear to be connected to line length, but the way the indentation differs, so I am pretty sure that the multi_line_output
setting is the problem (I have it on 3). In spite of the setting, isort
wants this:
from utils._transactions import (get_bin_int_rels_tx,
get_interactor_to_organism_edges_tx,
get_nodes_tx)
while black
wants this (as I understand it, this should be multi_line_output=3
):
from utils._transactions import (
get_bin_int_rels_tx,
get_interactor_to_organism_edges_tx,
get_nodes_tx,
)
Can anyone help? @timothycrosley
Have the same problem with isort and black:
The following setup still produces a dead lock between the two:
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
Ok I configured black to use a line-length of 80 and the solution was to configure the same value for isort otherweise both will deadlock each other even so the profile was set to black.
Ok I configured black to use a line-length of 80 and the solution was to configure the same value for isort otherweise both will deadlock each other even so the profile was set to black.
I'm adding isort to my project that already runs pre-commit and black and I'm experiencing the same thing right now.
I was able to make it work with the following config:
- Related modules pip freeze
pre-commit==2.16.0
black==21.12b0
isort==5.10.1
- pyproject.toml
[tool.black]
line-length = 79
target-version = ['py310']
include = '\.pyi?$'
[tool.isort]
profile = "black"
line_length = 79
- pre-commit hooks
- repo: https://github.com/psf/black
rev: 21.12b0
hooks:
- id: black
name: black-api
files: api/
args: [--config, api/pyproject.toml]
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort-api
files: api/
args: [--settings-path, api/pyproject.toml]
Removing the line length config for isort create the "deadlock" ehavior mentioned several times in this thread, which is weird because 79 is the default value according to isort documentation.
I also experienced conflicts between the two when using black as a profile. The commands that I run are:
pre-commit run --all-files
git commit -m "..."
In my case I had this in my pyproject.toml file:
[tool.isort]
profile = "black"
and this as a pre-commit hook:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
exclude: site-packages/
args: ["--profile", "black"]
removing the keyword tool
from the pyproject.toml file was the fix in my case.
I also experienced conflicts. I found a solution that I haven't seen documented anywhere yet
- repo: 'https://github.com/psf/black'
rev: 22.6.0
hooks:
- id: black
args: [--line-length=80]
- repo: https://github.com/pycqa/isort
rev: 5.11.5
hooks:
- id: isort
args: ["--profile=black"]
the difference is the argument format of isort args: ["--profile=black"]
I didn't want to inject configuration via the command-line arguments so that filesystem-stored settings are honored.
It took a bit of combat due to a default behavior that isort
has that's a bit baffling to me, but can be modified via the --resolve-all-configs
argument. For posterity:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
# pre-commit passes all changed files to a single invocation of isort.
# isort's default behavior is to select the configuration file that
# simultaneously applies to all the files it took as arguments, rather
# than formatting each file to its own closest config.
args: ["--resolve-all-configs"]
- repo: 'https://github.com/psf/black-pre-commit-mirror'
rev: 23.11.0
hooks:
- id: black
This worked for me!
- repo: https://github.com/psf/black
rev: 22.10.0 # Use the latest version of Black
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0 # Use the latest version of isort
hooks:
- id: isort
args: ["--profile", "black"]
If using the VS Code extension for isort from Microsoft, add --profile=black
to the Isort: Args
setting.