Isort: How to make isort black compatible. Original Question: isort conflicts with black?

Created on 2 Oct 2020  路  4Comments  路  Source: PyCQA/isort

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?

documentation question

Most helpful comment

@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

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings