Skip to content

Preserve input order for equal-key top-N results - #242

Draft
AlexXuz wants to merge 1 commit into
maxfischer2781:masterfrom
AlexXuz:fix/topn-stable-ties
Draft

AlexXuz wants to merge 1 commit into
maxfischer2781:masterfrom
AlexXuz:fix/topn-stable-ties

Conversation

@AlexXuz

@AlexXuz AlexXuz commented Oct 28, 2026

Copy link
Copy Markdown

This PR preserves input order for equal-key records returned by nlargest and nsmallest.

  • Use decreasing encounter indices in both heaps, retaining earlier items at the cutoff and returning ties in arrival order.
  • Preserve key equality, including the same-object identity shortcut, through ReverseLT so tuple comparisons can use their encounter index.
  • Add 62 parametrized cases covering both functions, async keys, dict payloads, heap replacement, limits larger than the input, and a shared non-reflexive key.

Closes #241

Validation on Linux:

  • Original base: 44 new cases fail, 18 pass.
  • CPython 3.12.14: 464 tests pass.
  • CPython 3.13.5: 460 pass, 4 existing version-specific descriptor skips.
  • Black, flake8, mypy and pyright pass.

Other Python versions and operating systems were not run locally. The change does not modify merge or the active work in #238. AI-assisted contribution with independent correctness review.

Use decreasing encounter indices and preserve reverse-key equality.
Add regression coverage for stable ties and shared non-reflexive keys.

AI-assisted implementation and independent review.
Refs maxfischer2781#241
@maxfischer2781 maxfischer2781 self-assigned this Oct 28, 2026
@maxfischer2781

Copy link
Copy Markdown
Owner

Hi @AlexXuz, thanks for the bug report and solution draft. Please be mindful that AI-assisted contributions don't have the best track record here and that I am aware your attention is spread over dozens of PRs.

I have given your PR a first run of inspections. Please mark it as ready for review once appropriate.

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.

[Bug]: nlargest and nsmallest reverse equal-key input order

2 participants