Victor Sotillo
Victor Sotillo

Reputation: 87

isort black strange behavior

When applying isort 5.12.0 in pre-commit within a python file, it re-orders the imports in bad. In bitbucket pipelines, the same code orders correctly.

This correct code:

from dagster import build_init_resource_context
from module1 import setting
from module1.resources.apple import AppleConnector as apple_connector
from module1.resources.apple import apple_resource
from module1.samples.apple import (
    apple_schema as apple_schemas,
)
from jsonschema import validate

Gets re-ordered this way:

from dagster import build_init_resource_context
from module1.resources.apple import AppleConnector as apple_connector
from module1.resources.apple import apple_resource
from module1.samples.apple import (
    apple_schema as apple_schemas,
)
from jsonschema import validate

from module1 import setting

¿Why is it happening?

Upvotes: 3

Views: 4785

Answers (3)

SmackDH
SmackDH

Reputation: 11

Personally the only thing that worked for me in the end was to create a .isort.cfg and set the profile there, and then specify the path to my settings file in .pre-commit-config.yaml.

.isort.cfg

[settings]
profile=black

.pre-commit-config.yaml

- repo: https://github.com/timothycrosley/isort
    rev: 5.13.2
    hooks:
      - id: isort
        args: ['--settings-path', 'api/.isort.cfg']

Upvotes: 0

Victor Sotillo
Victor Sotillo

Reputation: 87

In .pre-commit-config.yaml isort configuration, I had to add module1 as a first-party package. That avoided different interpretations in different repositories. Then pre-commit worked consistently in all the pipelines.

-   repo: https://github.com/PyCQA/isort
    rev: 5.12.0
    hooks:
    -   id: isort
        args: ["--profile", "black", "--filter-files", "--project", "module1"]

Upvotes: 0

Anton Petrov
Anton Petrov

Reputation: 381

Make sure you specify black as a profile in the isort configuration to avoid any conflicts.

[tool.isort]
profile = "black"

Or if you're using pre-commit:

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

Also, check out this article: https://pycqa.github.io/isort/docs/configuration/black_compatibility.html

Upvotes: 9

Related Questions