isort: How to make isort black compatible. Original Question: isort conflicts with black?
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?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 48
- Comments: 20 (3 by maintainers)
Commits related to this issue
- update conflicting isort and black config see https://github.com/PyCQA/isort/issues/1518 — committed to samsja/pytorch-vision-transformers by samsja 3 years ago
- update conflicting isort and black config see https://github.com/PyCQA/isort/issues/1518 — committed to samsja/pytorch-boilerplate by samsja 3 years ago
- bugfix: added isort --profile=black see https://github.com/PyCQA/isort/issues/1518 — committed to theislab/batchglm by picciama 2 years ago
- Fix isort/black conflict Refs: - https://github.com/PyCQA/isort/issues/1518 modified: .pre-commit-config.yaml — committed to tallamjr/astronet by tallamjr 2 years ago
- Use "black" profile when running isort (commit hooks) There are some incompatibilities between the standard configurations for isort and black ([ref](https://github.com/PyCQA/isort/issues/1518). ... — committed to developmentseed/eoAPI by leothomas 2 years ago
- Try to fix an isort<>black conflict based on https://github.com/PyCQA/isort/issues/1518 — committed to DoodleScheduling/pre-commit-config-updater by deleted user 2 years ago
- make isort black-compatible `isort` rules sometimes conflict with `black` rules, and compatibility must be specified manually. See this issue: https://github.com/PyCQA/isort/issues/1518 — committed to dlqqq/extension-cookiecutter by dlqqq 2 years ago
- Make `isort` compatible with `black`. More info: https://github.com/PyCQA/isort/issues/1518 — committed to choosistant/choosistant-template by sheikhomar 2 years ago
- Make `isort` compatible with `black`. More info: https://github.com/PyCQA/isort/issues/1518 — committed to choosistant/algorithms by sheikhomar 2 years ago
- Set isort profile argument to "black" There are some corner-cases where isort and black revert each other's changes and you can't resolve your code such that they're both happy. There's a related iss... — committed to JSv4/cookiecutter-django by JSv4 a year ago
- Fix: isort and black conflict ref: https://github.com/PyCQA/isort/issues/1518 — committed to shenxiangzhuang/mppt by shenxiangzhuang 9 months ago
- Fix: isort and black conflict ref: https://github.com/PyCQA/isort/issues/1518 — committed to shenxiangzhuang/mppt by shenxiangzhuang 9 months ago
- isort/black wars... See https://github.com/PyCQA/isort/issues/1518 — committed to Mathics3/mathics-scanner by rocky 4 months ago
@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:
Alternatively, you could update your precommit to set the profile when running isort:
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!
you can choose the
blackprofile inisortthat should solve the conflict. You could include that in the.isort.cfgfile in your repository.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:
Ah my bad, I was doing it wrong somehow. Got it working now with and
.isort.cfgthat has this. The example I followed above was for a toml file.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:
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.
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
@alexisgaziello couldnāt you just set it something other than 72? Whatever youāre preferred line length is? Am I missing something?
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.
thanks for posting this, helped š
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:
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