Skip to content

Commit 63d6dd5

Browse files
committed
Add default to anext helper
1 parent f78eb96 commit 63d6dd5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pymongo/asynchronous/helpers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ async def inner(*args: Any, **kwargs: Any) -> Any:
7373
aiter = builtins.aiter
7474
else:
7575

76-
async def anext(cls: Any) -> Any:
76+
async def anext(cls: Any, default: Any) -> Any:
7777
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#anext."""
78-
return await cls.__anext__()
78+
try:
79+
return await cls.__anext__()
80+
except StopAsyncIteration:
81+
if default:
82+
return default
83+
raise
7984

8085
def aiter(cls: Any) -> Any:
8186
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#anext."""

pymongo/synchronous/helpers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ def inner(*args: Any, **kwargs: Any) -> Any:
7373
iter = builtins.iter
7474
else:
7575

76-
def next(cls: Any) -> Any:
76+
def next(cls: Any, default: Any) -> Any:
7777
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#next."""
78-
return cls.__next__()
78+
try:
79+
return cls.__next__()
80+
except StopIteration:
81+
if default:
82+
return default
83+
raise
7984

8085
def iter(cls: Any) -> Any:
8186
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#next."""

0 commit comments

Comments
 (0)