Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions asyncstdlib/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Optional,
Dict,
Any,
cast,
)
import builtins as _sync_builtins

Expand Down Expand Up @@ -446,13 +447,11 @@ async def sorted(
It is guaranteed to be worst-case O(n log n) runtime.
"""
if key is None:
# TODO: is this a worthwhile optimisation?
try:
return _sync_builtins.sorted(iterable, reverse=reverse) # type: ignore
except TypeError:
items: _sync_builtins.list[Any] = [item async for item in aiter(iterable)]
items.sort(reverse=reverse)
return items
if isinstance(iterable, Iterable):
return _sync_builtins.sorted(cast(Iterable[Any], iterable), reverse=reverse)
items: _sync_builtins.list[Any] = [item async for item in aiter(iterable)]
items.sort(reverse=reverse)
return items
else:
async_key = _awaitify(key)
keyed_items = [(await async_key(item), item) async for item in aiter(iterable)]
Expand Down
26 changes: 26 additions & 0 deletions unittests/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,29 @@ def collision_key(x: int) -> int:
assert await a.sorted(values, key=awaitify(collision_key)) == sorted(
values, key=collision_key
)


@pytest.mark.parametrize("reverse", [False, True])
@sync
async def test_sorted_preserves_comparison_errors(reverse):
with pytest.raises(TypeError):
await a.sorted(iter([1, "two"]), reverse=reverse)


@sync
async def test_sorted_preserves_iteration_errors():
def records():
yield 1
raise TypeError("source failure")

with pytest.raises(TypeError, match="source failure"):
await a.sorted(records())


@sync
async def test_sorted_supports_indexed_sequences():
class Indexed:
def __getitem__(self, index):
return [3, 1, 2][index]

assert await a.sorted(Indexed()) == [1, 2, 3]
Loading