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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:33
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

201reactions
timothycrosleycommented, Oct 3, 2020

@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

38reactions
tordbbcommented, Oct 5, 2020

Thanks, this solution worked beautifully!

- repo: https://github.com/pycqa/isort
    rev: 5.5.4
    hooks:
      - id: isort
        args: ["--profile", "black"]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to run isort formatter from black command in python
isort helps to sort and format imports in your Python code. Black also formats imports, but in a different way from isort's defaults...
Read more >
Using Black with other tools - Black 22.12.0 documentation
isort helps to sort and format imports in your Python code. Black also formats imports, but in a different way from isort's defaults...
Read more >
Making isort compatible with black - A code to remember
The main difference between isort and black are on there points: the multi line mode; the trailing comma of the last import; the...
Read more >
black · PyPI
Runtime support for Python 3.6 has been removed. Formatting 3.6 code will still be supported until further notice. Stable style. Fix a crash...
Read more >
Coding style - Django documentation
If the error was with black or isort then the tool will go ahead and fix them for you. ... Use flake8 to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found