Is it possible to run isort formatter from black command in python
Asked Answered
C

2

6

I like to get inspiration from well designed python projects.

The last one that inspired me was the poetry repository.

I copied a lot from that, but the subject of this post are black and isort.

Both are well configured in pyproject.toml:

[tool.isort]
profile = "black"
...
known_first_party = "poetry"


[tool.black]
line-length = 88
include = '\.pyi?$'
exclude = '''
/(
...
)/
'''

and formatting is configured in the Makefileas:

format: clean
    @poetry run black poetry/ tests/

I thought that running make format would run blackwhich would internally run isort, but when I ran isort ., it correctly formated the import statements afterwards. It then seems black did not run isort.

Question: does black run isort internally?

Chanson answered 9/8, 2021 at 12:53 Comment(1)
Ok, then how does poetry run isort on formatting?Chanson
D
9

Question: does black run isort internally?

No, it doesn't.

isort has a profile = "black" option that makes it adhere to Black's standards though.

The poetry repository itself has a pre-commit hook defined here in .pre-commit-config.yaml that makes sure isort is run (along with a couple of other tools).

Drip answered 9/8, 2021 at 12:56 Comment(0)
K
3

No, it doesn't run isort.

As noted in this document, Using Black with other tools:

isort

isort helps to sort and format imports in your Python code. Black also formats imports, but in a different way from isort's defaults which leads to conflicting changes.

Keats answered 9/8, 2021 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.