PYTHON-3359 Remove Database and Collection timeout override (#1009)

Remove MongoClient.timeout in favor of client.options.timeout.
This commit is contained in:
Shane Harvey 2022-07-18 19:54:45 -05:00 committed by GitHub
parent c43486101f
commit 5c38676d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 42 additions and 8215 deletions

View File

@ -58,7 +58,7 @@ class _TimeoutContext(object):
Use :func:`pymongo.timeout` instead::
with client.timeout(0.5):
with pymongo.timeout(0.5):
client.test.test.insert_one({})
"""

View File

@ -265,7 +265,7 @@ class ClientOptions(object):
@property
def timeout(self) -> Optional[float]:
"""The timeout.
"""The configured timeoutMS converted to seconds, or None.
..versionadded: 4.2
"""

View File

@ -116,7 +116,6 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
write_concern: Optional[WriteConcern] = None,
read_concern: Optional["ReadConcern"] = None,
session: Optional["ClientSession"] = None,
timeout: Optional[float] = None,
**kwargs: Any,
) -> None:
"""Get / create a Mongo collection.
@ -201,7 +200,6 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
read_preference or database.read_preference,
write_concern or database.write_concern,
read_concern or database.read_concern,
timeout if timeout is not None else database.timeout,
)
if not isinstance(name, str):
raise TypeError("name must be an instance of str")
@ -395,7 +393,6 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
read_preference: Optional[_ServerMode] = None,
write_concern: Optional[WriteConcern] = None,
read_concern: Optional["ReadConcern"] = None,
timeout: Optional[float] = None,
) -> "Collection[_DocumentType]":
"""Get a clone of this collection changing the specified settings.
@ -434,7 +431,6 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
read_preference or self.read_preference,
write_concern or self.write_concern,
read_concern or self.read_concern,
timeout=timeout if timeout is not None else self.timeout,
)
def bulk_write(

View File

@ -831,7 +831,6 @@ class BaseObject(object):
read_preference: _ServerMode,
write_concern: WriteConcern,
read_concern: ReadConcern,
timeout: Optional[float],
) -> None:
if not isinstance(codec_options, CodecOptions):
raise TypeError("codec_options must be an instance of bson.codec_options.CodecOptions")
@ -855,12 +854,6 @@ class BaseObject(object):
raise TypeError("read_concern must be an instance of pymongo.read_concern.ReadConcern")
self.__read_concern = read_concern
if not isinstance(timeout, (int, float, type(None))):
raise TypeError("timeout must be None, an int, or a float")
if timeout and timeout < 0:
raise TypeError("timeout cannot be negative")
self.__timeout = float(timeout) if timeout else None
@property
def codec_options(self) -> CodecOptions:
"""Read only access to the :class:`~bson.codec_options.CodecOptions`
@ -910,14 +903,6 @@ class BaseObject(object):
"""
return self.__read_concern
@property
def timeout(self) -> Optional[float]:
"""Read only access to the timeout of this instance.
.. versionadded:: 4.2
"""
return self.__timeout
class _CaseInsensitiveDictionary(abc.MutableMapping):
def __init__(self, *args, **kwargs):

View File

@ -76,7 +76,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
read_preference: Optional[_ServerMode] = None,
write_concern: Optional["WriteConcern"] = None,
read_concern: Optional["ReadConcern"] = None,
timeout: Optional[float] = None,
) -> None:
"""Get a database by client and name.
@ -129,7 +128,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
read_preference or client.read_preference,
write_concern or client.write_concern,
read_concern or client.read_concern,
timeout if timeout is not None else client.timeout,
)
if not isinstance(name, str):
@ -157,7 +155,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
read_preference: Optional[_ServerMode] = None,
write_concern: Optional["WriteConcern"] = None,
read_concern: Optional["ReadConcern"] = None,
timeout: Optional[float] = None,
) -> "Database[_DocumentType]":
"""Get a clone of this database changing the specified settings.
@ -197,7 +194,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
read_preference or self.read_preference,
write_concern or self.write_concern,
read_concern or self.read_concern,
timeout if timeout is not None else self.timeout,
)
def __eq__(self, other: Any) -> bool:
@ -246,7 +242,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
read_preference: Optional[_ServerMode] = None,
write_concern: Optional["WriteConcern"] = None,
read_concern: Optional["ReadConcern"] = None,
timeout: Optional[float] = None,
) -> Collection[_DocumentType]:
"""Get a :class:`~pymongo.collection.Collection` with the given name
and options.
@ -293,7 +288,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
read_preference,
write_concern,
read_concern,
timeout=timeout,
)
def create_collection(
@ -304,7 +298,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
write_concern: Optional["WriteConcern"] = None,
read_concern: Optional["ReadConcern"] = None,
session: Optional["ClientSession"] = None,
timeout: Optional[float] = None,
check_exists: Optional[bool] = True,
**kwargs: Any,
) -> Collection[_DocumentType]:
@ -459,7 +452,6 @@ class Database(common.BaseObject, Generic[_DocumentType]):
write_concern,
read_concern,
session=s,
timeout=timeout,
**kwargs,
)

View File

@ -789,7 +789,6 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]):
options.read_preference,
options.write_concern,
options.read_concern,
options.timeout,
)
self._topology_settings = TopologySettings(
@ -1955,7 +1954,6 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]):
read_preference: Optional[_ServerMode] = None,
write_concern: Optional[WriteConcern] = None,
read_concern: Optional["ReadConcern"] = None,
timeout: Optional[float] = None,
) -> database.Database[_DocumentType]:
"""Get a :class:`~pymongo.database.Database` with the given name and
options.
@ -2006,7 +2004,7 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]):
name = self.__default_database_name
return database.Database(
self, name, codec_options, read_preference, write_concern, read_concern, timeout
self, name, codec_options, read_preference, write_concern, read_concern
)
def _database_default_options(self, name):

View File

@ -61,7 +61,8 @@
"useMultipleMongoses": false,
"uriOptions": {
"appName": "reduceMaxTimeMSTest",
"w": 1
"w": 1,
"timeoutMS": 500
},
"observeEvents": [
"commandStartedEvent"
@ -75,35 +76,16 @@
"databaseName": "test"
}
},
{
"collection": {
"id": "regularCollection",
"database": "database",
"collectionName": "coll"
}
},
{
"collection": {
"id": "timeoutCollection",
"database": "database",
"collectionName": "timeoutColl",
"collectionOptions": {
"timeoutMS": 60
}
"collectionName": "timeoutColl"
}
}
]
}
},
{
"name": "insertOne",
"object": "regularCollection",
"arguments": {
"document": {
"_id": 1
}
}
},
{
"name": "insertOne",
"object": "timeoutCollection",
@ -118,18 +100,6 @@
{
"client": "client",
"events": [
{
"commandStartedEvent": {
"commandName": "insert",
"databaseName": "test",
"command": {
"insert": "coll",
"maxTimeMS": {
"$$exists": false
}
}
}
},
{
"commandStartedEvent": {
"commandName": "insert",
@ -137,7 +107,7 @@
"command": {
"insert": "timeoutColl",
"maxTimeMS": {
"$$lte": 60
"$$lte": 500
}
}
}
@ -180,7 +150,8 @@
"useMultipleMongoses": false,
"uriOptions": {
"appName": "rttTooHighTest",
"w": 1
"w": 1,
"timeoutMS": 10
},
"observeEvents": [
"commandStartedEvent"
@ -194,21 +165,11 @@
"databaseName": "test"
}
},
{
"collection": {
"id": "regularCollection",
"database": "database",
"collectionName": "coll"
}
},
{
"collection": {
"id": "timeoutCollection",
"database": "database",
"collectionName": "timeoutColl",
"collectionOptions": {
"timeoutMS": 2
}
"collectionName": "timeoutColl"
}
}
]
@ -216,11 +177,38 @@
},
{
"name": "insertOne",
"object": "regularCollection",
"object": "timeoutCollection",
"arguments": {
"document": {
"_id": 1
"_id": 2
}
},
"expectError": {
"isTimeoutError": true
}
},
{
"name": "insertOne",
"object": "timeoutCollection",
"arguments": {
"document": {
"_id": 2
}
},
"expectError": {
"isTimeoutError": true
}
},
{
"name": "insertOne",
"object": "timeoutCollection",
"arguments": {
"document": {
"_id": 2
}
},
"expectError": {
"isTimeoutError": true
}
},
{
@ -239,20 +227,7 @@
"expectEvents": [
{
"client": "client",
"events": [
{
"commandStartedEvent": {
"commandName": "insert",
"databaseName": "test",
"command": {
"insert": "coll",
"maxTimeMS": {
"$$exists": false
}
}
}
}
]
"events": []
}
]
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1160,6 +1160,7 @@ class UnifiedSpecTestMixinV1(IntegrationTest):
raise NotImplementedError
elif isinstance(target, ClientEncryption):
method_name = "_clientEncryptionOperation_%s" % (opname,)
client = target._key_vault_client
else:
method_name = "doesNotExist"
@ -1175,7 +1176,7 @@ class UnifiedSpecTestMixinV1(IntegrationTest):
try:
# TODO: PYTHON-3289 apply inherited timeout by default.
inherit_timeout = getattr(target, "timeout", None)
inherit_timeout = client.options.timeout
# CSOT: Translate the spec test "timeout" arg into pymongo's context timeout API.
if "timeout" in arguments or inherit_timeout is not None:
timeout = arguments.pop("timeout", None)