diff --git a/pymongo/asynchronous/mongo_client.py b/pymongo/asynchronous/mongo_client.py index 9dba97d12..f7fc8e5e8 100644 --- a/pymongo/asynchronous/mongo_client.py +++ b/pymongo/asynchronous/mongo_client.py @@ -1185,7 +1185,7 @@ class AsyncMongoClient(common.BaseObject, Generic[_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 21fa57b5d..5786bbf5a 100644 --- a/pymongo/synchronous/mongo_client.py +++ b/pymongo/synchronous/mongo_client.py @@ -1185,7 +1185,7 @@ class MongoClient(common.BaseObject, Generic[_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}" diff --git a/test/asynchronous/test_auth.py b/test/asynchronous/test_auth.py index e516ff679..06f7fb9ca 100644 --- a/test/asynchronous/test_auth.py +++ b/test/asynchronous/test_auth.py @@ -169,7 +169,7 @@ class TestGSSAPI(unittest.IsolatedAsyncioTestCase): client = AsyncMongoClient(mech_uri) await client[GSSAPI_DB].collection.find_one() - set_name = await client.admin.command(HelloCompat.LEGACY_CMD).get("setName") + set_name = async_client_context.replica_set_name if set_name: if not self.service_realm_required: # Without authMechanismProperties @@ -242,7 +242,7 @@ class TestGSSAPI(unittest.IsolatedAsyncioTestCase): thread.join() self.assertTrue(thread.success) - set_name = await client.admin.command(HelloCompat.LEGACY_CMD).get("setName") + set_name = async_client_context.replica_set_name if set_name: client = AsyncMongoClient( GSSAPI_HOST, @@ -296,7 +296,7 @@ class TestSASLPlain(unittest.IsolatedAsyncioTestCase): client = AsyncMongoClient(uri) await client.ldap.test.find_one() - set_name = await client.admin.command(HelloCompat.LEGACY_CMD).get("setName") + set_name = async_client_context.replica_set_name if set_name: client = AsyncMongoClient( SASL_HOST, diff --git a/test/test_auth.py b/test/test_auth.py index 0bf0cfd80..fa3d0905b 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -169,7 +169,7 @@ class TestGSSAPI(unittest.TestCase): client = MongoClient(mech_uri) client[GSSAPI_DB].collection.find_one() - set_name = client.admin.command(HelloCompat.LEGACY_CMD).get("setName") + set_name = client_context.replica_set_name if set_name: if not self.service_realm_required: # Without authMechanismProperties @@ -242,7 +242,7 @@ class TestGSSAPI(unittest.TestCase): thread.join() self.assertTrue(thread.success) - set_name = client.admin.command(HelloCompat.LEGACY_CMD).get("setName") + set_name = client_context.replica_set_name if set_name: client = MongoClient( GSSAPI_HOST, @@ -296,7 +296,7 @@ class TestSASLPlain(unittest.TestCase): client = MongoClient(uri) client.ldap.test.find_one() - set_name = client.admin.command(HelloCompat.LEGACY_CMD).get("setName") + set_name = client_context.replica_set_name if set_name: client = MongoClient( SASL_HOST,