force_sort_within_sections does not sort lazy imports by module name

Đang mở Phù hợp với người mới
#2,811 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@rahul05ranjan đang làm issue này rồi.

Từ ngày 25/11/2026.

  • #2813 của @rahul05ranjan — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
cli

Hướng nghiên cứu

Khóa sắp xếp được lấy từ section_key() trong isort/sorting.py, hàm này loại bỏ tiền tố ^from và ^import nhưng không loại bỏ lazy , nên mọi dòng lazy đều lấy lazy làm khóa. Hãy bắt đầu bằng cách tái hiện lỗi với ví dụ hai dòng trong issue, rồi đọc section_key() để xem khóa được tạo ra như thế nào. Test hiện có test_force_sort_within_sections_applies_to_lazy trong bộ test dùng ast/zlib nên che giấu lỗi này, vì vậy hãy thêm một trường hợp mà thứ tự module khác với cách tách from/import. Công việc hoàn tất khi lazy import aa đứng trước lazy from zz import x và các test hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
7k
Fork
838
Merge trung bình
2 ngày 6 giờ
Pull request đã merge (30 ngày)
19

Chuẩn bị môi trường

  • Có Dockerfile hoặc tệp Docker Compose
  • Không có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của PyCQA/isort

Tất cả issue của PyCQA/isort

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.