Skip to content

--fss may lead to incorrect import separation for mixed multi-line and alias imports #2455

@n0gu-furiosa

Description

@n0gu-furiosa

When using --force-sort-within-sections (--fss), imports from the same module can be incorrectly separated.

Reproduction (repro.py):

from module import (
    AAAAAAAAAAAAAAAAAAAAAAAAAA,
    BBBBBBBBBBBBBBBBBBBBBBBBBB,
    CCCCCCCCCCCCCCCCCCCCCCCCCC,
)
from module import DDDDDDDDDDDDDDDDDDDDDDDDDD as d
from module import (
    EEEEEEEEEEEEEEEEEEEEEEEEEE,
    FFFFFFFFFFFFFFFFFFFFFFFFFF,
    GGGGGGGGGGGGGGGGGGGGGGGGGG,
)

Sorting this file with isort repro.py --fss results in imports from the same module being split in a seemingly meaningless way:

from module import (
    AAAAAAAAAAAAAAAAAAAAAAAAAA,
    BBBBBBBBBBBBBBBBBBBBBBBBBB,
    CCCCCCCCCCCCCCCCCCCCCCCCCC,
)
from module import (
    EEEEEEEEEEEEEEEEEEEEEEEEEE,
    FFFFFFFFFFFFFFFFFFFFFFFFFF,
    GGGGGGGGGGGGGGGGGGGGGGGGGG,
)
from module import DDDDDDDDDDDDDDDDDDDDDDDDDD as d

This happens because _with_from_imports sees those as three separate groups, and then during within-section sorting sorting.section_key determines that import (... should precede import D....

It's hard to say that isort is doing something wrong, but splitting imports from the same module is undesirable. My preferred output in this situation would be:

from module import (
    AAAAAAAAAAAAAAAAAAAAAAAAAA,
    BBBBBBBBBBBBBBBBBBBBBBBBBB,
    CCCCCCCCCCCCCCCCCCCCCCCCCC,
    EEEEEEEEEEEEEEEEEEEEEEEEEE,
    FFFFFFFFFFFFFFFFFFFFFFFFFF,
    GGGGGGGGGGGGGGGGGGGGGGGGGG,
)
from module import DDDDDDDDDDDDDDDDDDDDDDDDDD as d

I think there are a few ways to approach this:

  • Add a new configuration option: Introduce a flag (e.g., --force-separate-as-imports) that operates within the _with_from_imports function to explicitly place as_imports at the end of the list.
  • Normalize ( in section_key: Modify the key generation so that module import (AAAA is treated as module import AAAA for sorting purposes. This would not produce my "preferred output," but it would at least leave repro.py unchanged, avoiding multiple separated from module import ( blocks.
  • No new flag, no new sort logic, just merge outputs from new_section_output: Iterate over the new section output, parse them again, and merge imports where possible. I personally think this would be the least preferred option because it doesn't integrate well with the rest of the system and would have a high maintenance cost.

I would appreciate any input from the maintainers on whether this is worth addressing and, if so, which approach is preferred. I am happy to contribute a PR if the team agrees on a direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions