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

Most upvoted comments

@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"]

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.

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]

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

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.

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.

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 šŸ™

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

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