Fix sorted() to use __lt__ instead of __gt__ by youknowone · Pull Request #6887 · RustPython/RustPython · GitHub
Skip to content

Fix sorted() to use __lt__ instead of __gt__ - #6887

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:sorted
Jan 28, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowone:sorted

Conversation

@youknowone

@youknowone youknowone commented Jan 27, 2026

Copy link
Copy Markdown
Member

CPython's sort uses lt for comparisons, but RustPython was using gt. This caused issues when only lt was overridden on a subclass (e.g., NamedTuple with custom lt), as it would fall back to the parent class's comparison instead of using the overridden method.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed sorting behavior to consistently use the __lt__ comparison method, aligning with CPython semantics for more predictable results when sorting custom objects.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] test: cpython/Lib/test/test_peepholer.py (TODO: 32)

dependencies:

dependent tests: (no tests depend on peepholer)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone
youknowone marked this pull request as ready for review January 28, 2026 01:24
@github-actions

Copy link
Copy Markdown
Contributor

CPython's sort uses __lt__ for comparisons, but RustPython was using
__gt__. This caused issues when only __lt__ was overridden on a
subclass (e.g., NamedTuple with custom __lt__), as it would fall back
to the parent class's comparison instead of using the overridden method.
@youknowone
youknowone enabled auto-merge (squash) January 28, 2026 07:55
@youknowone
youknowone disabled auto-merge January 28, 2026 07:55
@youknowone
youknowone merged commit d2166dd into RustPython:main Jan 28, 2026
10 of 13 checks passed
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.

1 participant