Skip to content

Commit 17a8154

Browse files
authored
PYTHON-4594 - Add to_list documentation (mongodb#1757)
1 parent 6020ae4 commit 17a8154

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

doc/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ PyMongo 4.9 brings a number of improvements including:
1111
- Add support for :attr:`~pymongo.encryption.Algorithm.RANGE` and deprecate
1212
:attr:`~pymongo.encryption.Algorithm.RANGEPREVIEW`.
1313
- pymongocrypt>=1.10 is now required for :ref:`In-Use Encryption` support.
14+
- Added :meth:`~pymongo.cursor.Cursor.to_list` to :class:`~pymongo.cursor.Cursor`,
15+
:class:`~pymongo.command_cursor.CommandCursor`,
16+
:class:`~pymongo.asynchronous.cursor.AsyncCursor`,
17+
and :class:`~pymongo.asynchronous.command_cursor.AsyncCommandCursor`
18+
as an asynchronous-friendly alternative to ``list(cursor)``.
19+
1420

1521
Issues Resolved
1622
...............

pymongo/asynchronous/command_cursor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
382382
await self.close()
383383

384384
async def to_list(self) -> list[_DocumentType]:
385+
"""Converts the contents of this cursor to a list more efficiently than ``[doc async for doc in cursor]``.
386+
387+
To use::
388+
389+
>>> await cursor.to_list()
390+
391+
If the cursor is empty or has no more results, an empty list will be returned.
392+
393+
.. versionadded:: 4.9
394+
"""
385395
res: list[_DocumentType] = []
386396
while self.alive:
387397
if not await self._next_batch(res):

pymongo/asynchronous/cursor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,16 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
12871287
await self.close()
12881288

12891289
async def to_list(self) -> list[_DocumentType]:
1290+
"""Converts the contents of this cursor to a list more efficiently than ``[doc async for doc in cursor]``.
1291+
1292+
To use::
1293+
1294+
>>> await cursor.to_list()
1295+
1296+
If the cursor is empty or has no more results, an empty list will be returned.
1297+
1298+
.. versionadded:: 4.9
1299+
"""
12901300
res: list[_DocumentType] = []
12911301
while self.alive:
12921302
if not await self._next_batch(res):

pymongo/synchronous/command_cursor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
382382
self.close()
383383

384384
def to_list(self) -> list[_DocumentType]:
385+
"""Converts the contents of this cursor to a list more efficiently than ``[doc async for doc in cursor]``.
386+
387+
To use::
388+
389+
>>> await cursor.to_list()
390+
391+
If the cursor is empty or has no more results, an empty list will be returned.
392+
393+
.. versionadded:: 4.9
394+
"""
385395
res: list[_DocumentType] = []
386396
while self.alive:
387397
if not self._next_batch(res):

pymongo/synchronous/cursor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,16 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
12851285
self.close()
12861286

12871287
def to_list(self) -> list[_DocumentType]:
1288+
"""Converts the contents of this cursor to a list more efficiently than ``[doc async for doc in cursor]``.
1289+
1290+
To use::
1291+
1292+
>>> await cursor.to_list()
1293+
1294+
If the cursor is empty or has no more results, an empty list will be returned.
1295+
1296+
.. versionadded:: 4.9
1297+
"""
12881298
res: list[_DocumentType] = []
12891299
while self.alive:
12901300
if not self._next_batch(res):

0 commit comments

Comments
 (0)