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.
When using
--force-sort-within-sections(--fss), imports from the same module can be incorrectly separated.Reproduction (
repro.py):Sorting this file with
isort repro.py --fssresults in imports from the same module being split in a seemingly meaningless way:This happens because
_with_from_importssees those as three separate groups, and then during within-section sortingsorting.section_keydetermines thatimport (...should precedeimport 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:
I think there are a few ways to approach this:
--force-separate-as-imports) that operates within the_with_from_importsfunction to explicitly place as_imports at the end of the list.(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.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.