diff --git a/gridfs/asynchronous/grid_file.py b/gridfs/asynchronous/grid_file.py index 4d6140750e..afc1a0f756 100644 --- a/gridfs/asynchronous/grid_file.py +++ b/gridfs/asynchronous/grid_file.py @@ -1176,20 +1176,22 @@ def __getattr__(self, name: str) -> Any: 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 @@ def __init__( _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 @@ async def readchunk(self) -> bytes: """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) diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index bc2e29a61d..80015f96e7 100644 --- a/gridfs/synchronous/grid_file.py +++ b/gridfs/synchronous/grid_file.py @@ -1166,20 +1166,22 @@ def __getattr__(self, name: str) -> Any: 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 @@ def __init__( _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 @@ def readchunk(self) -> bytes: """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) diff --git a/pyproject.toml b/pyproject.toml index 4380b57e8d..8452bfe956 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"