-
-
Notifications
You must be signed in to change notification settings - Fork 625
--fss may lead to incorrect import separation for mixed multi-line and alias imports #2455
Description
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 dThis 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 dI 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_importsfunction to explicitly place as_imports at the end of the list. - Normalize
(in section_key: Modify the key generation so thatmodule import (AAAAis treated asmodule import AAAAfor sorting purposes. This would not produce my "preferred output," but it would at least leave repro.py unchanged, avoiding multiple separatedfrom 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.