Skip to content
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

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

Merged
merged 14 commits into from
Sep 4, 2024

Conversation

NoahStapp
Copy link
Contributor

No description provided.

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.

pymongo/synchronous/topology.py Outdated Show resolved Hide resolved
test/test_grid_file.py Outdated Show resolved Hide resolved
blink1073
blink1073 previously approved these changes Aug 30, 2024
Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM!

"""Read one line or up to `size` bytes from the file.

:param size: the maximum number of bytes to read
"""
return await self._read_size_or_line(size=size, line=True)

async def readlines(self, size: int = -1) -> list[bytes]:
Copy link
Member

Choose a reason for hiding this comment

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

This should be async only too since we intentionally get it for free from IOBase in the sync version.

@@ -1194,19 +1194,9 @@ def __setattr__(self, name: str, value: Any) -> None:
)
Copy link
Member

Choose a reason for hiding this comment

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

Rather than having __setattr__ always raise an error in async, we should allow it as long as the file is not closed and only raise an error if the file is closed:

                # 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:        
                    if _IS_SYNC:
                        self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
                    else:
                        raise AttributeError(
                            "AsyncGridIn does not support __setattr__ after being closed(). Set the attribute before closing the file or use AsyncGridIn.set() instead")

# self.assertEqual(data, g.read(10) + g.read(10))
# return True
#
# qcheck.check_unittest(self, helper, qcheck.gen_string(qcheck.gen_range(0, 20)))
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't disable this test in sync. It should only be skipped in async.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, forgot to uncomment whoops.

@@ -781,6 +848,7 @@ def test_zip(self):
self.assertEqual(1, self.db.fs.chunks.count_documents({}))

g = GridOut(self.db.fs, f._id)
g.open()
Copy link
Member

Choose a reason for hiding this comment

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

Why add the open call here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Artifact from before I made this test sync-only, good catch.

if _IS_SYNC:
a.filename = "my_file"
else:
a.set("filename", "my_file")
Copy link
Member

Choose a reason for hiding this comment

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

Most (all?) of these can be change back to the a.filename = "my_file" version now, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trying to use a.filename = "my_file" gives the following error:

AttributeError: property 'filename' of 'AsyncGridIn' object has no setter

This is because all those properties are _a_grid_in_property instances, which don't have an async setter since setting does file IO.

Copy link
Member

Choose a reason for hiding this comment

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

We should make the setters work now. They will only raise an error on async when the file is closed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I misunderstood--makes sense, will do.

@@ -213,12 +226,14 @@ def test_grid_out_default_opts(self):

gout = GridOut(self.db.fs, 5)
with self.assertRaises(NoFile):
gout.open()
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of added open() calls in this file. Is that intended or should they be changed to if not _IS_SYNC: open()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They should all be only for the async tests, fixed.

@ShaneHarvey
Copy link
Member

LGTM but could you update the PR title and jira ticket to summarize the gridfs changes?

@NoahStapp NoahStapp changed the title PYTHON-4669 - Update More APIs for Motor Compatibility PYTHON-4669 - Update Async GridFS APIs for Motor Compatibility Sep 4, 2024
@NoahStapp NoahStapp merged commit 4e74c82 into mongodb:master Sep 4, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants