Skip to content

Evaluate untyped_sorted keys once per item - #552

Closed
Gambit-Checkmate wants to merge 1 commit into
mahmoud:masterfrom
Gambit-Checkmate:fix/untyped-sort-key-once
Closed

Gambit-Checkmate wants to merge 1 commit into
mahmoud:masterfrom
Gambit-Checkmate:fix/untyped-sort-key-once

Conversation

@Gambit-Checkmate

@Gambit-Checkmate Gambit-Checkmate commented Oct 7, 2026 •

Copy link
Copy Markdown

The change

Evaluate untyped_sorted's key once per input item, before comparisons, like sorted. Previously comparison-time evaluation repeatedly ran expensive or stateful keys and skipped singleton key errors. Cache the key in the existing wrapper; retain mixed-type fallback ordering and stable equal-key order. Document the behavior and cover generators, ascending/descending mixed keys, singleton errors, and stable ties.

Verification on Windows/Python 3.13.13: the new tests fail before the fix (3 failures, 1 pass), then python -m pytest --doctest-modules boltons tests -q passes: 682 tests and 12 subtests. No runtime dependencies added.

The backstory

This contribution was investigated and prepared by Codex at the account owner's request to contribute tested fixes to established open-source projects. I found the problem while inspecting sorting helpers after checking candidate issues and existing PRs; I do not claim that the human contributor encountered it in a personal application. The architecture guidance's standard-library consistency rule informed the comparison with sorted.

AI assistance

  • Harness/tooling: Codex, GitHub CLI, Python and pytest.
  • Model(s): OpenAI GPT-6.
  • Original prompt(s): “每天要提交pr,今天还是提交两个”. The standing task requires upstream repositories with at least 1,000 stars, duplicate checks, real changes, sufficient validation and verified write permissions before submission.

Fixes #551.

GitHub Actions is awaiting maintainer approval for this external contribution (action_required): https://github.com/mahmoud/boltons/actions/runs/37569573870 . Local full validation passed; Read the Docs preview build passed.

@Saadia751 Saadia751 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked a key that records its calls while sorting a mixture of numbers and strings. Each original item is evaluated once, the returned values remain the original items, and equal-key order stays stable with reverse either way. A key exception also propagates rather than being swallowed.

Checked 03688670cd45 with standalone Python 3.12.14 checks (9 passing assertions). I haven't run the full upstream suite.

mahmoud added a commit that referenced this pull request Oct 25, 2026
…ize, falsey keys, key-once untyped_sorted, negative maxsplit, fractional backoff; add chunked_filter (#585, #569, #563, #554, #552, #502, #496, #491)
@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

@mahmoud mahmoud closed this Oct 25, 2026
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.

untyped_sorted repeats key evaluation and skips singleton key errors

3 participants