简体   繁体   中英

How can I keep grouped imports by module with isort for renamed imports?

I have the following import:

from PyPDF2.constants import (
    CatalogAttributes as CA,
    Core as CO,
    DocumentInformationAttributes as DI,
    EncryptionDictAttributes as ED,
    PageAttributes as PG,
    PagesAttributes as PA,
    StreamAttributes as SA,
    TrailerKeys as TK,
    CatalogDictionary as CD,
)

I want to use isort and I want to keep the vertical hanging indent . It works fine for all other imports, but this one just always gets converted to:

from PyPDF2.constants import CatalogAttributes as CA
from PyPDF2.constants import CatalogDictionary as CD
from PyPDF2.constants import Core as CO
from PyPDF2.constants import DocumentInformationAttributes as DI
from PyPDF2.constants import EncryptionDictAttributes as ED
from PyPDF2.constants import PageAttributes as PG
from PyPDF2.constants import PagesAttributes as PA
from PyPDF2.constants import StreamAttributes as SA
from PyPDF2.constants import TrailerKeys as TK

How can I make isort keep / enforce this grouping by the module from which it was imported? Why is it even doing that with my given configuration?

This is my .isort.cfg :

[settings]
line_length=79
indent='    '
multi_line_output=3
length_sort=0
include_trailing_comma=True

It seems that there is an option that you need:

combine_as_imports = true

My .toml config looks like:

[tool.isort]
multi_line_output = 3
combine_as_imports = true
include_trailing_comma = false

force_grid_wrap = 2

use_parentheses = true
ensure_newline_before_comments = true
line_length = 110
indent = 4
atomic = true
case_sensitive = false
balanced_wrapping = false

And the result of isort applied for your code is:

from PyPDF2.constants import (
    CatalogAttributes as CA,
    CatalogDictionary as CD,
    Core as CO,
    DocumentInformationAttributes as DI,
    EncryptionDictAttributes as ED,
    PageAttributes as PG,
    PagesAttributes as PA,
    StreamAttributes as SA,
    TrailerKeys as TK
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM