From 336c8cb044723a06119a220a94d9b8ae87321d0d Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Thu, 12 Sep 2024 11:12:34 -0400 Subject: [PATCH] PYTHON-4758 - Only emit warnings for unclosed clients after opening --- pymongo/asynchronous/mongo_client.py | 2 +- pymongo/synchronous/mongo_client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pymongo/asynchronous/mongo_client.py b/pymongo/asynchronous/mongo_client.py index 9dba97d12a..f7fc8e5e81 100644 --- a/pymongo/asynchronous/mongo_client.py +++ b/pymongo/asynchronous/mongo_client.py @@ -1185,7 +1185,7 @@ def __getitem__(self, name: str) -> database.AsyncDatabase[_DocumentType]: def __del__(self) -> None: """Check that this AsyncMongoClient has been closed and issue a warning if not.""" try: - if not self._closed: + if self._opened and not self._closed: warnings.warn( ( f"Unclosed {type(self).__name__} opened at:\n{self._topology_settings._stack}" diff --git a/pymongo/synchronous/mongo_client.py b/pymongo/synchronous/mongo_client.py index 21fa57b5d8..5786bbf5a9 100644 --- a/pymongo/synchronous/mongo_client.py +++ b/pymongo/synchronous/mongo_client.py @@ -1185,7 +1185,7 @@ def __getitem__(self, name: str) -> database.Database[_DocumentType]: def __del__(self) -> None: """Check that this MongoClient has been closed and issue a warning if not.""" try: - if not self._closed: + if self._opened and not self._closed: warnings.warn( ( f"Unclosed {type(self).__name__} opened at:\n{self._topology_settings._stack}"