Skip to content

Preserve synchronous sorted iteration and comparison errors - #232

Closed
Apeksha9389 wants to merge 1 commit into
maxfischer2781:masterfrom
Apeksha9389:fix/preserve-sorted-errors
Closed

Apeksha9389 wants to merge 1 commit into
maxfischer2781:masterfrom
Apeksha9389:fix/preserve-sorted-errors

Conversation

@Apeksha9389

@Apeksha9389 Apeksha9389 commented Oct 19, 2026 •

Copy link
Copy Markdown

This PR fixes sorted() silently returning [] when a synchronous iterator raises TypeError during iteration or comparison. For example, await sorted(iter([1, "two"])) currently consumes the iterator, catches the comparison error, retries the exhausted iterator as async input, and returns an empty result.

Select the synchronous fast path based on the iterable interface instead of catching every TypeError from the entire sort. Async input still uses the existing materialization path, including synchronous sequence-protocol objects without __iter__.

Three new regression cases fail on master and pass here. They cover both sort directions for incomparable values and an iterator that raises TypeError itself. A positive control covers indexed sequences.

Validation on Python 3.12: 406 tests pass. Repository-wide Flake8, Black, MyPy and PyRight pass. Other interpreter versions are left to CI.

Prepared with AI assistance; reproduced and tested locally.

Closes #236

@Apeksha9389

Copy link
Copy Markdown
Author

Could a maintainer approve the pending CI runs and review this PR for merging? Local tests, Flake8, Black, MyPy, and PyRight all pass. Thank you.

@maxfischer2781 maxfischer2781 self-assigned this Oct 19, 2026
@maxfischer2781

Copy link
Copy Markdown
Owner

See #230 (comment)

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]: sorted swallows TypeError from synchronous generators

2 participants