force_sort_within_sections does not sort lazy imports by module name
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
The sort key comes from section_key() in isort/sorting.py, which strips the ^from and ^import prefixes but not lazy , so every lazy line keys on lazy. Start by reproducing with the two-line example from the issue and read section_key() to see how the key is built. The existing test test_force_sort_within_sections_applies_to_lazy in the test suite uses ast/zlib, which hides the bug, so add a case where module order differs from the from/import split. Done when lazy import aa precedes lazy from zz import x and the existing tests still pass.
Written by the indexing model from the issue text.
Description
Description
When force_sort_within_sections is enabled, PEP 810 lazy imports are not
sorted by module name. Instead, every lazy from ... import ... line is
placed before every lazy import ... line, regardless of module names.
Steps to reproduce
With isort 9.0.2, create example.py:
lazy import aa
lazy from zz import x
and run:
$ isort --force-sort-within-sections example.py
Actual behavior
lazy from zz import x
lazy import aa
The from zz line sorts before lazy import aa, even though zz > aa.
The equivalent eager imports are ordered correctly:
$ printf 'import aa\nfrom zz import x\n' > eager.py && isort --force-sort-within-sections eager.py
import aa
from zz import x
Expected behavior
lazy import aa
lazy from zz import x
i.e. the same module-name ordering as the eager equivalent.
A slightly bigger example, all-lazy:
lazy import z
lazy from m import a
lazy import a.b
lazy from n import q as r
actual output:
lazy from m import a
lazy from n import q as r
lazy import a.b
lazy import z
while the eager equivalent is merged and sorted by module (a.b, m, n, z).
Suspected cause
section_key() in isort/sorting.py strips the ^from /^import prefixes
before deriving the sort key, but a lazy import line starts with lazy , so
neither regex matches. The module name then resolves to lazy for every lazy
import, and the full line is used as the key, which makes lazy from ...
(sorting as lazy f...) always precede lazy import ... (lazy i...).
The existing test for this option
(test_force_sort_within_sections_applies_to_lazy) uses ast/zlib, where
module order coincides with the from/straight split, so the mis-ordering is
not caught.
- Dominant language
- Python
- Stars
- 7k
- Forks
- 838
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 19
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/isort
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
length_sort with native sort order puts a longer import firstPossibly taken @simpleqt claimed this 24 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
PyCQA/isort#2698 · 2 comments ·
Maintainers usually reply within 2 days
-
Cache compiled known-module patterns across Config instancesPossibly taken @Yin123-ybh claimed this 2 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
PyCQA/isort#2805 · 1 comment ·
Maintainers usually reply within 2 days
-
add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)Possibly taken @Tanvi-007 claimed this 6 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
PyCQA/isort#2799 · 2 comments ·
Maintainers usually reply within 2 days
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single importPossibly taken @beduldul claimed this 29 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 15/100
PyCQA/isort#2752 · 1 comment ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
TigreGotico/text2spoken#626 ·
Maintainers usually reply within 1 day