Skip to content

fix(sorting): preserve length order with native sorting - #2703

Open
EchoJohn wants to merge 1 commit into
PyCQA:mainfrom
EchoJohn:fix/2698-native-length-sort
Open

EchoJohn wants to merge 1 commit into
PyCQA:mainfrom
EchoJohn:fix/2698-native-length-sort

Conversation

@EchoJohn

@EchoJohn EchoJohn commented Oct 2, 2026

Copy link
Copy Markdown

With length_sort=True and sort_order="native", import bbbbbbbbbb is placed before import a. The length prefix in the sort key is compared as text, so multi-digit lengths do not follow numeric order. The same problem affects imported names and force_sort_within_sections.

Zero-pad length prefixes to the width of sys.maxsize in both module and section keys. This covers the possible range of len() while keeping the string key interface used by sorting plugins. Natural ordering, equal-length tie breaking, reverse sorting, type groups and force_to_top retain their behavior.

Fixes #2698.

Validation on Windows with Python 3.12.7, against d93953fa5c9f188db31b9006cb681de1126e5967:

  • The 24 new regression cases produced 11 failures / 13 passes before the fix and all pass afterward.
  • python -X utf8 -m coverage run --parallel -m pytest tests/unit -q -ra --tb=short: 693 passed / 5 skipped. The skips are existing platform restrictions and an existing duplicate-import TODO.
  • Combined coverage: 99% overall, 100% for isort/sorting.py, including branches.
  • Ruff checks and formatting, Flake8, Bandit and git diff --check pass.
  • Import sorting checks pass using --skip example_crlf_file.py to apply the already-configured fixture exclusion by basename on Windows.
  • Mypy passes for all 87 source files with --platform linux, matching the lint job's target platform, and for the two changed files with native Windows settings. Native Windows checking of the entire tree reports os.mkfifo in the unchanged tests/unit/test_settings.py fixture.
  • Other Python versions, compiled wheels and the separate integration/property-test suite were not run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

length_sort with native sort order puts a longer import first

1 participant