Merge branch 'master' of github.com:mongodb/mongo-python-driver

This commit is contained in:
Steven Silvester 2024-09-12 09:10:04 -05:00
commit 9f83a750cb
No known key found for this signature in database
GPG Key ID: B1BF5EC3A8B32F91
9 changed files with 37 additions and 17 deletions

View File

@ -231,7 +231,9 @@ class AsyncCollection(common.BaseObject, Generic[_DocumentType]):
from pymongo.asynchronous.database import AsyncDatabase
if not isinstance(database, AsyncDatabase):
raise TypeError(f"AsyncCollection requires an AsyncDatabase but {type(database)} given")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "AsyncDatabase" for cls in type(database).__mro__):
raise TypeError(f"AsyncDatabase required but given {type(database).__name__}")
if not name or ".." in name:
raise InvalidName("collection names cannot be empty")

View File

@ -125,7 +125,9 @@ class AsyncDatabase(common.BaseObject, Generic[_DocumentType]):
raise TypeError("name must be an instance of str")
if not isinstance(client, AsyncMongoClient):
raise TypeError(f"AsyncMongoClient required but given {type(client)}")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "AsyncMongoClient" for cls in type(client).__mro__):
raise TypeError(f"AsyncMongoClient required but given {type(client).__name__}")
if name != "$external":
_check_name(name)

View File

@ -597,7 +597,13 @@ class AsyncClientEncryption(Generic[_DocumentType]):
raise TypeError("codec_options must be an instance of bson.codec_options.CodecOptions")
if not isinstance(key_vault_client, AsyncMongoClient):
raise TypeError(f"AsyncMongoClient required but given {type(key_vault_client)}")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(
cls.__name__ == "AsyncMongoClient" for cls in type(key_vault_client).__mro__
):
raise TypeError(
f"AsyncMongoClient required but given {type(key_vault_client).__name__}"
)
self._kms_providers = kms_providers
self._key_vault_namespace = key_vault_namespace
@ -685,9 +691,9 @@ class AsyncClientEncryption(Generic[_DocumentType]):
"""
if not isinstance(database, AsyncDatabase):
raise TypeError(
f"create_encrypted_collection() requires an AsyncDatabase but {type(database)} given"
)
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "AsyncDatabase" for cls in type(database).__mro__):
raise TypeError(f"AsyncDatabase required but given {type(database).__name__}")
encrypted_fields = deepcopy(encrypted_fields)
for i, field in enumerate(encrypted_fields["fields"]):

View File

@ -2446,7 +2446,9 @@ class _MongoClientErrorHandler:
self, client: AsyncMongoClient, server: Server, session: Optional[AsyncClientSession]
):
if not isinstance(client, AsyncMongoClient):
raise TypeError(f"AsyncMongoClient required but given {type(client)}")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "AsyncMongoClient" for cls in type(client).__mro__):
raise TypeError(f"AsyncMongoClient required but given {type(client).__name__}")
self.client = client
self.server_address = server.description.address

View File

@ -234,7 +234,9 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
from pymongo.synchronous.database import Database
if not isinstance(database, Database):
raise TypeError(f"Collection requires a Database but {type(database)} given")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "Database" for cls in type(database).__mro__):
raise TypeError(f"Database required but given {type(database).__name__}")
if not name or ".." in name:
raise InvalidName("collection names cannot be empty")

View File

@ -125,7 +125,9 @@ class Database(common.BaseObject, Generic[_DocumentType]):
raise TypeError("name must be an instance of str")
if not isinstance(client, MongoClient):
raise TypeError(f"MongoClient required but given {type(client)}")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "MongoClient" for cls in type(client).__mro__):
raise TypeError(f"MongoClient required but given {type(client).__name__}")
if name != "$external":
_check_name(name)

View File

@ -595,7 +595,9 @@ class ClientEncryption(Generic[_DocumentType]):
raise TypeError("codec_options must be an instance of bson.codec_options.CodecOptions")
if not isinstance(key_vault_client, MongoClient):
raise TypeError(f"MongoClient required but given {type(key_vault_client)}")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "MongoClient" for cls in type(key_vault_client).__mro__):
raise TypeError(f"MongoClient required but given {type(key_vault_client).__name__}")
self._kms_providers = kms_providers
self._key_vault_namespace = key_vault_namespace
@ -683,9 +685,9 @@ class ClientEncryption(Generic[_DocumentType]):
"""
if not isinstance(database, Database):
raise TypeError(
f"create_encrypted_collection() requires a Database but {type(database)} given"
)
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "Database" for cls in type(database).__mro__):
raise TypeError(f"Database required but given {type(database).__name__}")
encrypted_fields = deepcopy(encrypted_fields)
for i, field in enumerate(encrypted_fields["fields"]):

View File

@ -2434,7 +2434,9 @@ class _MongoClientErrorHandler:
def __init__(self, client: MongoClient, server: Server, session: Optional[ClientSession]):
if not isinstance(client, MongoClient):
raise TypeError(f"MongoClient required but given {type(client)}")
# This is for compatibility with mocked and subclassed types, such as in Motor.
if not any(cls.__name__ == "MongoClient" for cls in type(client).__mro__):
raise TypeError(f"MongoClient required but given {type(client).__name__}")
self.client = client
self.server_address = server.description.address

View File

@ -580,7 +580,7 @@ class EntityMapUtil:
return
elif entity_type == "database":
client = self[spec["client"]]
if not isinstance(client, MongoClient):
if type(client).__name__ != "MongoClient":
self.test.fail(
"Expected entity {} to be of type MongoClient, got {}".format(
spec["client"], type(client)
@ -602,7 +602,7 @@ class EntityMapUtil:
return
elif entity_type == "session":
client = self[spec["client"]]
if not isinstance(client, MongoClient):
if type(client).__name__ != "MongoClient":
self.test.fail(
"Expected entity {} to be of type MongoClient, got {}".format(
spec["client"], type(client)
@ -667,7 +667,7 @@ class EntityMapUtil:
def get_listener_for_client(self, client_name: str) -> EventListenerUtil:
client = self[client_name]
if not isinstance(client, MongoClient):
if type(client).__name__ != "MongoClient":
self.test.fail(
f"Expected entity {client_name} to be of type MongoClient, got {type(client)}"
)