Skip to content

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.

@NoahStapp NoahStapp requested a review from ShaneHarvey August 30, 2024 15:25
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!

"""
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.

else:
raise AttributeError(
"AsyncGridIn does not support __setattr__. Use AsyncGridIn.set() instead"
)
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.

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.


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
3 participants