Skip to content

PYTHON-4669 - Update Async GridFS APIs for Motor Compatibility #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 4, 2024
18 changes: 11 additions & 7 deletions gridfs/asynchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,10 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any:
return False


class AsyncGridOut(io.IOBase):
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object


class AsyncGridOut(GRIDOUT_BASE_CLASS):
"""Class to read data out of GridFS."""

def __init__(
Expand Down Expand Up @@ -1486,13 +1489,14 @@ def __init__(
_file: Any
_chunk_iter: Any

async def __anext__(self) -> bytes:
return super().__next__()
if not _IS_SYNC:

def __next__(self) -> bytes: # noqa: F811, RUF100
if _IS_SYNC:
return super().__next__()
else:
async def __anext__(self) -> bytes:
return await self.readline()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to handle the end-of-stream and raise StopAsyncIteration here. Could you add a test for this?


# This is a duplicate definition of __next__ for the synchronous API
# due to the limitations of our synchro process
def __next__(self) -> bytes: # noqa: F811, RUF100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just remove __next__ altogether.

raise TypeError(
"AsyncGridOut does not support synchronous iteration. Use `async for` instead"
)
Expand Down
18 changes: 11 additions & 7 deletions gridfs/synchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,10 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any:
return False


class GridOut(io.IOBase):
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object


class GridOut(GRIDOUT_BASE_CLASS):
"""Class to read data out of GridFS."""

def __init__(
Expand Down Expand Up @@ -1474,13 +1477,14 @@ def __init__(
_file: Any
_chunk_iter: Any

def __next__(self) -> bytes:
return super().__next__()
if not _IS_SYNC:

def __next__(self) -> bytes: # noqa: F811, RUF100
if _IS_SYNC:
return super().__next__()
else:
def __next__(self) -> bytes:
return self.readline()

# This is a duplicate definition of __next__ for the synchronous API
# due to the limitations of our synchro process
def __next__(self) -> bytes: # noqa: F811, RUF100
raise TypeError("GridOut does not support synchronous iteration. Use `for` instead")

def open(self) -> None:
Expand Down
Loading