PYTHON-4669 - Update More APIs for Motor Compatibility (#1815)

This commit is contained in:
Noah Stapp 2024-08-27 13:38:42 -04:00 committed by GitHub
parent b8213f2817
commit 81ea92b808
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 17 deletions

View File

@ -1176,20 +1176,22 @@ class AsyncGridIn:
raise AttributeError("GridIn object has no attribute '%s'" % name)
def __setattr__(self, name: str, value: Any) -> None:
if _IS_SYNC:
# For properties of this instance like _buffer, or descriptors set on
# the class like filename, use regular __setattr__
if name in self.__dict__ or name in self.__class__.__dict__:
object.__setattr__(self, name, value)
else:
# For properties of this instance like _buffer, or descriptors set on
# the class like filename, use regular __setattr__
if name in self.__dict__ or name in self.__class__.__dict__:
object.__setattr__(self, name, value)
else:
if _IS_SYNC:
# All other attributes are part of the document in db.fs.files.
# Store them to be sent to server on close() or if closed, send
# them now.
self._file[name] = value
if self._closed:
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
else:
object.__setattr__(self, name, value)
else:
raise AttributeError(
"AsyncGridIn does not support __setattr__. Use AsyncGridIn.set() instead"
)
async def set(self, name: str, value: Any) -> None:
# For properties of this instance like _buffer, or descriptors set on
@ -1484,6 +1486,17 @@ class AsyncGridOut(io.IOBase):
_file: Any
_chunk_iter: Any
async def __anext__(self) -> bytes:
return super().__next__()
def __next__(self) -> bytes: # noqa: F811, RUF100
if _IS_SYNC:
return super().__next__()
else:
raise TypeError(
"AsyncGridOut does not support synchronous iteration. Use `async for` instead"
)
async def open(self) -> None:
if not self._file:
_disallow_transactions(self._session)
@ -1511,6 +1524,7 @@ class AsyncGridOut(io.IOBase):
"""Reads a chunk at a time. If the current position is within a
chunk the remainder of the chunk is returned.
"""
await self.open()
received = len(self._buffer) - self._buffer_pos
chunk_data = EMPTY
chunk_size = int(self.chunk_size)

View File

@ -1166,20 +1166,22 @@ class GridIn:
raise AttributeError("GridIn object has no attribute '%s'" % name)
def __setattr__(self, name: str, value: Any) -> None:
if _IS_SYNC:
# For properties of this instance like _buffer, or descriptors set on
# the class like filename, use regular __setattr__
if name in self.__dict__ or name in self.__class__.__dict__:
object.__setattr__(self, name, value)
else:
# For properties of this instance like _buffer, or descriptors set on
# the class like filename, use regular __setattr__
if name in self.__dict__ or name in self.__class__.__dict__:
object.__setattr__(self, name, value)
else:
if _IS_SYNC:
# All other attributes are part of the document in db.fs.files.
# Store them to be sent to server on close() or if closed, send
# them now.
self._file[name] = value
if self._closed:
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
else:
object.__setattr__(self, name, value)
else:
raise AttributeError(
"GridIn does not support __setattr__. Use GridIn.set() instead"
)
def set(self, name: str, value: Any) -> None:
# For properties of this instance like _buffer, or descriptors set on
@ -1472,6 +1474,15 @@ class GridOut(io.IOBase):
_file: Any
_chunk_iter: Any
def __next__(self) -> bytes:
return super().__next__()
def __next__(self) -> bytes: # noqa: F811, RUF100
if _IS_SYNC:
return super().__next__()
else:
raise TypeError("GridOut does not support synchronous iteration. Use `for` instead")
def open(self) -> None:
if not self._file:
_disallow_transactions(self._session)
@ -1499,6 +1510,7 @@ class GridOut(io.IOBase):
"""Reads a chunk at a time. If the current position is within a
chunk the remainder of the chunk is returned.
"""
self.open()
received = len(self._buffer) - self._buffer_pos
chunk_data = EMPTY
chunk_size = int(self.chunk_size)

View File

@ -126,7 +126,7 @@ module = ["service_identity.*"]
ignore_missing_imports = true
[[tool.mypy.overrides]]
module = ["pymongo.synchronous.*", "gridfs.synchronous.*"]
module = ["pymongo.synchronous.*"]
warn_unused_ignores = false
disable_error_code = ["unused-coroutine"]
@ -134,6 +134,10 @@ disable_error_code = ["unused-coroutine"]
module = ["pymongo.asynchronous.*"]
warn_unused_ignores = false
[[tool.mypy.overrides]]
module = ["gridfs.synchronous.*"]
warn_unused_ignores = false
disable_error_code = ["unused-coroutine", "no-redef"]
[tool.ruff]
target-version = "py37"