force_sort_within_sections does not sort lazy imports by module name
メンテナーはふだん 2 日以内に返信
評価
調査の方向性
ソートキーは 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 ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
PyCQA/isort のほかの issue
-
separate_packages splits imports from the same explicitly configured package対応中かも @rahul05ranjan が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
length_sort with native sort order puts a longer import first対応中かも @simpleqt が 24 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
Cache compiled known-module patterns across Config instances対応中かも @Yin123-ybh が 2 日前に担当しました。 オープン
難易度 3/5 半日 初心者へのやさしさ 30/100
メンテナーはふだん 2 日以内に返信
-
add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)対応中かも @Tanvi-007 が 6 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single import対応中かも @beduldul が 29 日前に担当しました。 オープン
難易度 1/5 1〜3時間 初心者へのやさしさ 15/100
メンテナーはふだん 2 日以内に返信
似ている issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
NatLabRockies/openstudio-mcp#286 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
intersystems/ipm#1338 ·
メンテナーはふだん 1 日以内に返信
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionオープンv1
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
TigreGotico/text2spoken#626 ·
メンテナーはふだん 1 日以内に返信