How to make isort always produce multi-line output when there are multiple imports on a line?
Asked Answered
H

1

9

I'm currently using isort --profile=black --line-length=79 as a linter in my project for python files.

This produces the Vertical Hanging Indent (mode 3 in isort's documentation kind of output:

from third_party import (
    lib1,
    lib2,
    lib3,
    lib4,
)

This multiline mode only applies if the line is longer than 79 characters, though. Is there a mode that cause a multiline output as soon as there are two or more imports on the same line, no matter how long the line is?

I tried hacking it with isort -m=3 --trailing-comma --line-length=1, but shorter line length will cause multiline output even when there is a single import, which I don't want:

from third_party import (
    lib1,
)
Hirokohiroshi answered 16/9, 2021 at 8:39 Comment(0)
H
9

You should use the --force-grid-wrap 2 flag in the CLI or set in the settings file like pyproject.toml option force_grid_wrap = 2. This would force isort to produce multiline output for 2 or more imports, regardless of line length. More info about this option

Haberdashery answered 7/3, 2022 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.