force_sort_within_sections does not sort lazy imports by module name

オープン 初心者向け
#2,811 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@rahul05ranjan がすでに取り組んでいます。

2026年11月25日 から。

  • #2813 @rahul05ranjan による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
cli

調査の方向性

ソートキーは isort/sorting.py の section_key() から生成されます。この関数は ^from と ^import のプレフィックスは取り除きますが lazy は取り除かないため、lazy の行はすべて lazy をキーとしてしまいます。まず issue にある 2 行の例で問題を再現し、section_key() を読んでキーがどのように組み立てられているかを確認してください。既存のテスト test_force_sort_within_sections_applies_to_lazy は ast/zlib を使っているためバグが隠れています。そこで、モジュールの順序が from/import の分割と食い違うケースを追加してください。lazy import aa が lazy from zz import x より前に来ること、そして既存のテストがすべて通ることを確認したら完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
7k
フォーク
838
平均マージ
2日 6時間
マージ済み PR(30日)
19

環境構築

  • Dockerfile または Docker Compose ファイルあり
  • プルリクエストのテンプレートなし
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PyCQA/isort のほかの issue

PyCQA/isort の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。