force_sort_within_sections does not sort lazy imports by module name

Open Beginner friendly
#2,811 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@rahul05ranjan is already working on this.

Since Nov 25, 2026.

  • #2813 by @rahul05ranjan — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyCQA/isort

All issues in PyCQA/isort

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.