Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2778 +/- ##
=======================================
Coverage 99.40% 99.40%
=======================================
Files 40 40
Lines 3367 3368 +1
Branches 726 726
=======================================
+ Hits 3347 3348 +1
Misses 12 12
Partials 8 8 🚀 New features to boost your workflow:
|
DanielNoord
left a comment
There was a problem hiding this comment.
Is there no better solution to this? This feels very hacky.
|
Good question. The constraint is that |
The sort keys built by module_key and section_key embed the import length as an unpadded decimal string, so with sort_order="native" (plain lexicographic sorted) "10:" compares before "9:" and longer imports come first once name lengths cross a digit boundary. The default natural order was unaffected because naturally() converts digit runs to ints. Zero-pad the embedded length to six digits so lexicographic key order equals numeric length order under both sorting functions. Six digits cover any realistic import line; natural-order keys are unchanged after int() strips the zeros, and sorting plugins keep receiving string keys. Fixes PyCQA#2698 Signed-off-by: simpleqt <2015900818@qq.com>
319728a to
e727430
Compare
|
I also rebased this onto current |
fix(sorting): keep length_sort in length order under native sort_order
Fixes #2698.
The problem
With
length_sort=Trueandsort_order="native", imports are no longer ordered by length: thesort key embeds the length as an unpadded decimal string (
str(len(name)) + ":" + nameinisort/sorting.py,module_key; plainlen(line)insection_key), and the native sort ordercompares those keys with plain lexicographic
sorted(). String comparison puts"10:"before"9:", so longer imports come first once name lengths cross a digit boundary:With the default
sort_order="natural"the same input sorts correctly (ifirst), becausenaturally()converts digit runs to integers via_natural_keys. The bug affects every lengthpath: straight imports,
length_sort_straight, from-import member names,length_sort_sections,force_sort_within_sections(viasection_key), andreverse_sort(which reverses the wrong order).
The input above carries no comments and no exotic syntax, so the defect does not depend on any
comment or docstring content.
The fix
Zero-pad the embedded length to a fixed width of six digits in both
module_keyandsection_key, so lexicographic key order equals numeric length order under plainsorted()while
naturally()keeps producing identical keys afterint()strips the zeros:Six digits cover any import line up to 999,999 characters; beyond that the key contains more
digits than the width and those entries only compare among themselves unpadded (same shape as
the pre-fix behavior, unreachable in practice).
This keeps the string-key interface that sorting plugins rely on (only the
sort_order="native"path, which is builtin
sorted, changes output), and preserves the documented nativelexicographic ordering of module names for equal-length imports (see
test_sort_configurable_sort_issue_1732).Why not the previously attempted padding
#2703 padded to
str(sys.maxsize)width (19 digits) and was closed after review asked forbenchmarks showing it did not slow sorting down (~9% full-pipeline slowdown was measured on that
approach). This patch pads to six digits using
str().zfill()instead of a format-specf-string, which benchmarks at parity:
module_keyvs the new one, sameprocess, 200 reps x 9, best-of, 1000 module names, Windows / CPython 3.11):
old 225.7 ms vs new 215.2 ms per run - parity (within run-to-run noise).
(
isort.codeof 1000 generated imports,length_sort=True, sort_order="native",number=30, repeat=7, best-of, median of 3 runs): main ~700 ms vs patched ~703 ms - parity.The unmodified paths (
sort_orderdefault, nolength_sort) also show no change.Behavior verification
A differential run over 120 deterministic 3-module import blocks and from-import blocks crossed
with 20 option sets (2520 cases) against
main@5d9c9619:sort_order="native"and a length flag).sort_order="native"andlength_sort/length_sort_straight/length_sort_sections- exactly the intended scope,with the patched output in nondecreasing import-name length order.
Test
tests/unit/test_isort.py::test_length_sort_native_order_issue_2698covers straight imports,reverse_sort,force_sort_within_sections(thesection_keypath),length_sort_straight,and from-import member names at a digit boundary (lengths 1, 8, 10). Red on
main, green withthis patch. Full suite: 741 passed, 9 skipped (2 collection errors from missing optional deps
in the local environment were resolved by installing the repo example plugins and
pytest-benchmark; unrelated to this change).
Proposed issue text
A companion issue already exists: #2698 ("length_sort with native sort order puts a longer
import first"). Do not file a new issue; reference #2698 in the PR ("Fixes #2698") and, if a
comment is wanted when submitting, paste: