Skip to content

Commit cbc2238

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-python-driver
2 parents 68e7da3 + 81ea92b commit cbc2238

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

gridfs/asynchronous/grid_file.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,20 +1176,22 @@ def __getattr__(self, name: str) -> Any:
11761176
raise AttributeError("GridIn object has no attribute '%s'" % name)
11771177

11781178
def __setattr__(self, name: str, value: Any) -> None:
1179-
if _IS_SYNC:
1180-
# For properties of this instance like _buffer, or descriptors set on
1181-
# the class like filename, use regular __setattr__
1182-
if name in self.__dict__ or name in self.__class__.__dict__:
1183-
object.__setattr__(self, name, value)
1184-
else:
1179+
# For properties of this instance like _buffer, or descriptors set on
1180+
# the class like filename, use regular __setattr__
1181+
if name in self.__dict__ or name in self.__class__.__dict__:
1182+
object.__setattr__(self, name, value)
1183+
else:
1184+
if _IS_SYNC:
11851185
# All other attributes are part of the document in db.fs.files.
11861186
# Store them to be sent to server on close() or if closed, send
11871187
# them now.
11881188
self._file[name] = value
11891189
if self._closed:
11901190
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
1191-
else:
1192-
object.__setattr__(self, name, value)
1191+
else:
1192+
raise AttributeError(
1193+
"AsyncGridIn does not support __setattr__. Use AsyncGridIn.set() instead"
1194+
)
11931195

11941196
async def set(self, name: str, value: Any) -> None:
11951197
# For properties of this instance like _buffer, or descriptors set on
@@ -1484,6 +1486,17 @@ def __init__(
14841486
_file: Any
14851487
_chunk_iter: Any
14861488

1489+
async def __anext__(self) -> bytes:
1490+
return super().__next__()
1491+
1492+
def __next__(self) -> bytes: # noqa: F811, RUF100
1493+
if _IS_SYNC:
1494+
return super().__next__()
1495+
else:
1496+
raise TypeError(
1497+
"AsyncGridOut does not support synchronous iteration. Use `async for` instead"
1498+
)
1499+
14871500
async def open(self) -> None:
14881501
if not self._file:
14891502
_disallow_transactions(self._session)
@@ -1511,6 +1524,7 @@ async def readchunk(self) -> bytes:
15111524
"""Reads a chunk at a time. If the current position is within a
15121525
chunk the remainder of the chunk is returned.
15131526
"""
1527+
await self.open()
15141528
received = len(self._buffer) - self._buffer_pos
15151529
chunk_data = EMPTY
15161530
chunk_size = int(self.chunk_size)

gridfs/synchronous/grid_file.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,20 +1166,22 @@ def __getattr__(self, name: str) -> Any:
11661166
raise AttributeError("GridIn object has no attribute '%s'" % name)
11671167

11681168
def __setattr__(self, name: str, value: Any) -> None:
1169-
if _IS_SYNC:
1170-
# For properties of this instance like _buffer, or descriptors set on
1171-
# the class like filename, use regular __setattr__
1172-
if name in self.__dict__ or name in self.__class__.__dict__:
1173-
object.__setattr__(self, name, value)
1174-
else:
1169+
# For properties of this instance like _buffer, or descriptors set on
1170+
# the class like filename, use regular __setattr__
1171+
if name in self.__dict__ or name in self.__class__.__dict__:
1172+
object.__setattr__(self, name, value)
1173+
else:
1174+
if _IS_SYNC:
11751175
# All other attributes are part of the document in db.fs.files.
11761176
# Store them to be sent to server on close() or if closed, send
11771177
# them now.
11781178
self._file[name] = value
11791179
if self._closed:
11801180
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
1181-
else:
1182-
object.__setattr__(self, name, value)
1181+
else:
1182+
raise AttributeError(
1183+
"GridIn does not support __setattr__. Use GridIn.set() instead"
1184+
)
11831185

11841186
def set(self, name: str, value: Any) -> None:
11851187
# For properties of this instance like _buffer, or descriptors set on
@@ -1472,6 +1474,15 @@ def __init__(
14721474
_file: Any
14731475
_chunk_iter: Any
14741476

1477+
def __next__(self) -> bytes:
1478+
return super().__next__()
1479+
1480+
def __next__(self) -> bytes: # noqa: F811, RUF100
1481+
if _IS_SYNC:
1482+
return super().__next__()
1483+
else:
1484+
raise TypeError("GridOut does not support synchronous iteration. Use `for` instead")
1485+
14751486
def open(self) -> None:
14761487
if not self._file:
14771488
_disallow_transactions(self._session)
@@ -1499,6 +1510,7 @@ def readchunk(self) -> bytes:
14991510
"""Reads a chunk at a time. If the current position is within a
15001511
chunk the remainder of the chunk is returned.
15011512
"""
1513+
self.open()
15021514
received = len(self._buffer) - self._buffer_pos
15031515
chunk_data = EMPTY
15041516
chunk_size = int(self.chunk_size)

pymongo/asynchronous/encryption.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ async def close(self) -> None:
304304

305305

306306
class RewrapManyDataKeyResult:
307-
"""Result object returned by a :meth:`~ClientEncryption.rewrap_many_data_key` operation.
307+
"""Result object returned by a :meth:`~AsyncClientEncryption.rewrap_many_data_key` operation.
308308
309309
.. versionadded:: 4.2
310310
"""
@@ -316,7 +316,7 @@ def __init__(self, bulk_write_result: Optional[BulkWriteResult] = None) -> None:
316316
def bulk_write_result(self) -> Optional[BulkWriteResult]:
317317
"""The result of the bulk write operation used to update the key vault
318318
collection with one or more rewrapped data keys. If
319-
:meth:`~ClientEncryption.rewrap_many_data_key` does not find any matching keys to rewrap,
319+
:meth:`~AsyncClientEncryption.rewrap_many_data_key` does not find any matching keys to rewrap,
320320
no bulk write operation will be executed and this field will be
321321
``None``.
322322
"""
@@ -506,7 +506,7 @@ def _create_mongocrypt_options(**kwargs: Any) -> MongoCryptOptions:
506506
return opts
507507

508508

509-
class ClientEncryption(Generic[_DocumentType]):
509+
class AsyncClientEncryption(Generic[_DocumentType]):
510510
"""Explicit client-side field level encryption."""
511511

512512
def __init__(
@@ -519,7 +519,7 @@ def __init__(
519519
) -> None:
520520
"""Explicit client-side field level encryption.
521521
522-
The ClientEncryption class encapsulates explicit operations on a key
522+
The AsyncClientEncryption class encapsulates explicit operations on a key
523523
vault collection that cannot be done directly on an AsyncMongoClient. Similar
524524
to configuring auto encryption on an AsyncMongoClient, it is constructed with
525525
an AsyncMongoClient (to a MongoDB cluster containing the key vault
@@ -1126,23 +1126,23 @@ async def rewrap_many_data_key(
11261126
result = await self._key_vault_coll.bulk_write(replacements)
11271127
return RewrapManyDataKeyResult(result)
11281128

1129-
async def __aenter__(self) -> ClientEncryption[_DocumentType]:
1129+
async def __aenter__(self) -> AsyncClientEncryption[_DocumentType]:
11301130
return self
11311131

11321132
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
11331133
await self.close()
11341134

11351135
def _check_closed(self) -> None:
11361136
if self._encryption is None:
1137-
raise InvalidOperation("Cannot use closed ClientEncryption")
1137+
raise InvalidOperation("Cannot use closed AsyncClientEncryption")
11381138

11391139
async def close(self) -> None:
11401140
"""Release resources.
11411141
11421142
Note that using this class in a with-statement will automatically call
11431143
:meth:`close`::
11441144
1145-
with ClientEncryption(...) as client_encryption:
1145+
with AsyncClientEncryption(...) as client_encryption:
11461146
encrypted = client_encryption.encrypt(value, ...)
11471147
decrypted = client_encryption.decrypt(encrypted)
11481148

pymongo/encryption_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(
7070
users. To configure automatic *decryption* without automatic
7171
*encryption* set ``bypass_auto_encryption=True``. Explicit
7272
encryption and explicit decryption is also supported for all users
73-
with the :class:`~pymongo.encryption.ClientEncryption` class.
73+
with the :class:`~pymongo.asynchronous.encryption.AsyncClientEncryption` and :class:`~pymongo.encryption.ClientEncryption` classes.
7474
7575
See :ref:`automatic-client-side-encryption` for an example.
7676

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,18 @@ module = ["service_identity.*"]
126126
ignore_missing_imports = true
127127

128128
[[tool.mypy.overrides]]
129-
module = ["pymongo.synchronous.*", "gridfs.synchronous.*"]
129+
module = ["pymongo.synchronous.*"]
130130
warn_unused_ignores = false
131131
disable_error_code = ["unused-coroutine"]
132132

133133
[[tool.mypy.overrides]]
134134
module = ["pymongo.asynchronous.*"]
135135
warn_unused_ignores = false
136136

137+
[[tool.mypy.overrides]]
138+
module = ["gridfs.synchronous.*"]
139+
warn_unused_ignores = false
140+
disable_error_code = ["unused-coroutine", "no-redef"]
137141

138142
[tool.ruff]
139143
target-version = "py37"

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"_AsyncGridOutChunkIterator": "GridOutChunkIterator",
5959
"_a_grid_in_property": "_grid_in_property",
6060
"_a_grid_out_property": "_grid_out_property",
61+
"AsyncClientEncryption": "ClientEncryption",
6162
"AsyncMongoCryptCallback": "MongoCryptCallback",
6263
"AsyncExplicitEncrypter": "ExplicitEncrypter",
6364
"AsyncAutoEncrypter": "AutoEncrypter",

0 commit comments

Comments
 (0)