Skip to content

Commit 81ea92b

Browse files
authored
PYTHON-4669 - Update More APIs for Motor Compatibility (#1815)
1 parent b8213f2 commit 81ea92b

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
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)

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"

0 commit comments

Comments
 (0)