Skip to content

Commit f69d330

Browse files
authored
PYTHON-4654 Clean up Async API to match Motor (mongodb#1789)
1 parent 47b2257 commit f69d330

18 files changed

+128
-121
lines changed

doc/api/pymongo/asynchronous/mongo_client.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
.. autoclass:: pymongo.asynchronous.mongo_client.AsyncMongoClient(host='localhost', port=27017, document_class=dict, tz_aware=False, connect=True, **kwargs)
88

9-
.. automethod:: aclose
9+
.. automethod:: close
1010

1111
.. describe:: c[db_name] || c.db_name
1212

pymongo/asynchronous/collection.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,9 +1893,7 @@ def find(self, *args: Any, **kwargs: Any) -> AsyncCursor[_DocumentType]:
18931893
"""
18941894
return AsyncCursor(self, *args, **kwargs)
18951895

1896-
async def find_raw_batches(
1897-
self, *args: Any, **kwargs: Any
1898-
) -> AsyncRawBatchCursor[_DocumentType]:
1896+
def find_raw_batches(self, *args: Any, **kwargs: Any) -> AsyncRawBatchCursor[_DocumentType]:
18991897
"""Query the database and retrieve batches of raw BSON.
19001898
19011899
Similar to the :meth:`find` method but returns a
@@ -1907,7 +1905,7 @@ async def find_raw_batches(
19071905
:mod:`bson` module.
19081906
19091907
>>> import bson
1910-
>>> cursor = await db.test.find_raw_batches()
1908+
>>> cursor = db.test.find_raw_batches()
19111909
>>> async for batch in cursor:
19121910
... print(bson.decode_all(batch))
19131911

pymongo/asynchronous/encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ async def close(self) -> None:
299299
self.client_ref = None
300300
self.key_vault_coll = None
301301
if self.mongocryptd_client:
302-
await self.mongocryptd_client.aclose()
302+
await self.mongocryptd_client.close()
303303
self.mongocryptd_client = None
304304

305305

@@ -439,7 +439,7 @@ async def close(self) -> None:
439439
self._closed = True
440440
await self._auto_encrypter.close()
441441
if self._internal_client:
442-
await self._internal_client.aclose()
442+
await self._internal_client.close()
443443
self._internal_client = None
444444

445445

pymongo/asynchronous/mongo_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ async def __aenter__(self) -> AsyncMongoClient[_DocumentType]:
13781378
return self
13791379

13801380
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
1381-
await self.aclose()
1381+
await self.close()
13821382

13831383
# See PYTHON-3084.
13841384
__iter__ = None
@@ -1514,7 +1514,7 @@ async def _end_sessions(self, session_ids: list[_ServerSession]) -> None:
15141514
# command.
15151515
pass
15161516

1517-
async def aclose(self) -> None:
1517+
async def close(self) -> None:
15181518
"""Cleanup client resources and disconnect from MongoDB.
15191519
15201520
End all server sessions created by this client by sending one or more
@@ -1541,6 +1541,10 @@ async def aclose(self) -> None:
15411541
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
15421542
await self._encrypter.close()
15431543

1544+
if not _IS_SYNC:
1545+
# Add support for contextlib.aclosing.
1546+
aclose = close
1547+
15441548
async def _get_topology(self) -> Topology:
15451549
"""Get the internal :class:`~pymongo.asynchronous.topology.Topology` object.
15461550

pymongo/synchronous/mongo_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,10 @@ def close(self) -> None:
15361536
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
15371537
self._encrypter.close()
15381538

1539+
if not _IS_SYNC:
1540+
# Add support for contextlib.closing.
1541+
aclose = close
1542+
15391543
def _get_topology(self) -> Topology:
15401544
"""Get the internal :class:`~pymongo.topology.Topology` object.
15411545

test/asynchronous/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async def _connect(self, host, port, **kwargs):
193193
self.connection_attempts.append(f"failed to connect client {client!r}: {exc}")
194194
return None
195195
finally:
196-
await client.aclose()
196+
await client.close()
197197

198198
async def _init_client(self):
199199
self.client = await self._connect(host, port)
@@ -409,7 +409,7 @@ async def _check_user_provided(self):
409409
else:
410410
raise
411411
finally:
412-
await client.aclose()
412+
await client.close()
413413

414414
def _server_started_with_auth(self):
415415
# MongoDB >= 2.0
@@ -1089,7 +1089,7 @@ async def async_teardown():
10891089
await c.drop_database("pymongo_test2")
10901090
await c.drop_database("pymongo_test_mike")
10911091
await c.drop_database("pymongo_test_bernie")
1092-
await c.aclose()
1092+
await c.close()
10931093

10941094
print_running_clients()
10951095

0 commit comments

Comments
 (0)