Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tordbb opened this issue Oct 2, 2020 · 21 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@tordbb
Copy link

tordbb commented Oct 2, 2020

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?

@anirudnits
Copy link
Collaborator

anirudnits commented Oct 2, 2020

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.

@timothycrosley
Copy link
Member

@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

@timothycrosley timothycrosley added question Further information is requested documentation Improvements or additions to documentation labels Oct 3, 2020
@tordbb
Copy link
Author

tordbb commented Oct 5, 2020

Thanks, this solution worked beautifully!

- repo: https://github.com/pycqa/isort
    rev: 5.5.4
    hooks:
      - id: isort
        args: ["--profile", "black"]

@timothycrosley timothycrosley pinned this issue Oct 5, 2020
@timothycrosley
Copy link
Member

timothycrosley commented Oct 5, 2020

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

@timothycrosley timothycrosley changed the title isort conflicts with black How to make isort black compatibe. Original Question: isort conflicts with black? Oct 5, 2020
@timothycrosley timothycrosley changed the title How to make isort black compatibe. Original Question: isort conflicts with black? How to make isort black compatible. Original Question: isort conflicts with black? Oct 5, 2020
@tordabp
Copy link

tordabp commented Jul 6, 2021

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]

@alexisgaziello
Copy link

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

@timothycrosley
Copy link
Member

@alexisgaziello couldn't you just set it something other than 72? Whatever you're preferred line length is? Am I missing something?

@mayurpande
Copy link

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

samsja added a commit to samsja/pytorch-vision-transformers that referenced this issue Oct 15, 2021
samsja added a commit to samsja/pytorch-boilerplate that referenced this issue Oct 15, 2021
picciama added a commit to theislab/batchglm that referenced this issue Feb 3, 2022
tallamjr added a commit to tallamjr/astronet that referenced this issue Feb 19, 2022
Refs:
    - PyCQA/isort#1518

	modified:   .pre-commit-config.yaml
leothomas added a commit to developmentseed/eoAPI that referenced this issue Mar 7, 2022
There are some incompatibilities between the standard configurations for isort and black ([ref](PyCQA/isort#1518). 

To avoid issues of black and isort continuously reformatting the same file, add the `--profile black` option to isort
sheikhomar added a commit to choosistant/algorithms that referenced this issue Sep 25, 2022
@mathiashls
Copy link

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.

JSv4 added a commit to JSv4/cookiecutter-django that referenced this issue Apr 13, 2023
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 issue in the iSort repo (cookiecutter#1518) (PyCQA/isort#1518). Appears the solution is to use the "black" profile, which can be set in setup.cfg or .pre-commit-config.yaml. This change adds the profile arg to the .pre-commit-config.yaml file
@meniluca
Copy link

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.

shenxiangzhuang added a commit to shenxiangzhuang/mppt that referenced this issue Sep 28, 2023
shenxiangzhuang added a commit to shenxiangzhuang/mppt that referenced this issue Sep 28, 2023
@calderon9505
Copy link

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

@nextsilicon-itay-bookstein

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

@TheMightyRaider
Copy link

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

@johncmacy
Copy link

If using the VS Code extension for isort from Microsoft, add --profile=black to the Isort: Args setting.

pcraig3 added a commit to HHS/simpler-grants-pdf-builder that referenced this issue Aug 12, 2024
Otherwise, isort and black would fight about the line lengths of
my import statements.

Issue here: PyCQA/isort#1518
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests