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}"