Skip to content

Commit 66e104c

Browse files
committed
WIP
1 parent 33867e5 commit 66e104c

File tree

7 files changed

+1014
-124
lines changed

7 files changed

+1014
-124
lines changed

gridfs/asynchronous/grid_file.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,19 +1194,9 @@ def __setattr__(self, name: str, value: Any) -> None:
11941194
)
11951195

11961196
async def set(self, name: str, value: Any) -> None:
1197-
# For properties of this instance like _buffer, or descriptors set on
1198-
# the class like filename, use regular __setattr__
1199-
if name in self.__dict__ or name in self.__class__.__dict__:
1200-
object.__setattr__(self, name, value)
1201-
else:
1202-
# All other attributes are part of the document in db.fs.files.
1203-
# Store them to be sent to server on close() or if closed, send
1204-
# them now.
1205-
self._file[name] = value
1206-
if self._closed:
1207-
await self._coll.files.update_one(
1208-
{"_id": self._file["_id"]}, {"$set": {name: value}}
1209-
)
1197+
self._file[name] = value
1198+
if self._closed:
1199+
await self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
12101200

12111201
async def _flush_data(self, data: Any, force: bool = False) -> None:
12121202
"""Flush `data` to a chunk."""
@@ -1400,10 +1390,11 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any:
14001390
return False
14011391

14021392

1403-
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object
1393+
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object # type: Any
1394+
14041395

1396+
class AsyncGridOut(GRIDOUT_BASE_CLASS): # type: ignore
14051397

1406-
class AsyncGridOut(GRIDOUT_BASE_CLASS):
14071398
"""Class to read data out of GridFS."""
14081399

14091400
def __init__(
@@ -1494,13 +1485,6 @@ def __init__(
14941485
async def __anext__(self) -> bytes:
14951486
return await self.readline()
14961487

1497-
# This is a duplicate definition of __next__ for the synchronous API
1498-
# due to the limitations of our synchro process
1499-
def __next__(self) -> bytes: # noqa: F811, RUF100
1500-
raise TypeError(
1501-
"AsyncGridOut does not support synchronous iteration. Use `async for` instead"
1502-
)
1503-
15041488
async def open(self) -> None:
15051489
if not self._file:
15061490
_disallow_transactions(self._session)
@@ -1620,7 +1604,7 @@ async def read(self, size: int = -1) -> bytes:
16201604
"""
16211605
return await self._read_size_or_line(size=size)
16221606

1623-
async def readline(self, size: int = -1) -> bytes: # type: ignore[override]
1607+
async def readline(self, size: int = -1) -> bytes:
16241608
"""Read one line or up to `size` bytes from the file.
16251609
16261610
:param size: the maximum number of bytes to read
@@ -1631,7 +1615,7 @@ def tell(self) -> int:
16311615
"""Return the current position of this file."""
16321616
return self._position
16331617

1634-
async def seek(self, pos: int, whence: int = _SEEK_SET) -> int: # type: ignore[override]
1618+
async def seek(self, pos: int, whence: int = _SEEK_SET) -> int:
16351619
"""Set the current position of this file.
16361620
16371621
:param pos: the position (or offset if using relative
@@ -1694,12 +1678,13 @@ def __aiter__(self) -> AsyncGridOut:
16941678
"""
16951679
return self
16961680

1697-
async def close(self) -> None: # type: ignore[override]
1681+
async def close(self) -> None:
16981682
"""Make GridOut more generically file-like."""
16991683
if self._chunk_iter:
17001684
await self._chunk_iter.close()
17011685
self._chunk_iter = None
1702-
super().close()
1686+
if _IS_SYNC:
1687+
super().close()
17031688

17041689
def write(self, value: Any) -> NoReturn:
17051690
raise io.UnsupportedOperation("write")

gridfs/synchronous/grid_file.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,17 +1184,9 @@ def __setattr__(self, name: str, value: Any) -> None:
11841184
)
11851185

11861186
def set(self, name: str, value: Any) -> None:
1187-
# For properties of this instance like _buffer, or descriptors set on
1188-
# the class like filename, use regular __setattr__
1189-
if name in self.__dict__ or name in self.__class__.__dict__:
1190-
object.__setattr__(self, name, value)
1191-
else:
1192-
# All other attributes are part of the document in db.fs.files.
1193-
# Store them to be sent to server on close() or if closed, send
1194-
# them now.
1195-
self._file[name] = value
1196-
if self._closed:
1197-
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
1187+
self._file[name] = value
1188+
if self._closed:
1189+
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
11981190

11991191
def _flush_data(self, data: Any, force: bool = False) -> None:
12001192
"""Flush `data` to a chunk."""
@@ -1388,10 +1380,11 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any:
13881380
return False
13891381

13901382

1391-
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object
1383+
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object # type: Any
1384+
13921385

1386+
class GridOut(GRIDOUT_BASE_CLASS): # type: ignore
13931387

1394-
class GridOut(GRIDOUT_BASE_CLASS):
13951388
"""Class to read data out of GridFS."""
13961389

13971390
def __init__(
@@ -1482,11 +1475,6 @@ def __init__(
14821475
def __next__(self) -> bytes:
14831476
return self.readline()
14841477

1485-
# This is a duplicate definition of __next__ for the synchronous API
1486-
# due to the limitations of our synchro process
1487-
def __next__(self) -> bytes: # noqa: F811, RUF100
1488-
raise TypeError("GridOut does not support synchronous iteration. Use `for` instead")
1489-
14901478
def open(self) -> None:
14911479
if not self._file:
14921480
_disallow_transactions(self._session)
@@ -1606,7 +1594,7 @@ def read(self, size: int = -1) -> bytes:
16061594
"""
16071595
return self._read_size_or_line(size=size)
16081596

1609-
def readline(self, size: int = -1) -> bytes: # type: ignore[override]
1597+
def readline(self, size: int = -1) -> bytes:
16101598
"""Read one line or up to `size` bytes from the file.
16111599
16121600
:param size: the maximum number of bytes to read
@@ -1617,7 +1605,7 @@ def tell(self) -> int:
16171605
"""Return the current position of this file."""
16181606
return self._position
16191607

1620-
def seek(self, pos: int, whence: int = _SEEK_SET) -> int: # type: ignore[override]
1608+
def seek(self, pos: int, whence: int = _SEEK_SET) -> int:
16211609
"""Set the current position of this file.
16221610
16231611
:param pos: the position (or offset if using relative
@@ -1680,12 +1668,13 @@ def __iter__(self) -> GridOut:
16801668
"""
16811669
return self
16821670

1683-
def close(self) -> None: # type: ignore[override]
1671+
def close(self) -> None:
16841672
"""Make GridOut more generically file-like."""
16851673
if self._chunk_iter:
16861674
self._chunk_iter.close()
16871675
self._chunk_iter = None
1688-
super().close()
1676+
if _IS_SYNC:
1677+
super().close()
16891678

16901679
def write(self, value: Any) -> NoReturn:
16911680
raise io.UnsupportedOperation("write")

test/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,11 @@ def tearDownClass(cls):
947947

948948
@classmethod
949949
def _setup_class(cls):
950-
cls._setup_class()
950+
pass
951951

952952
@classmethod
953953
def _tearDown_class(cls):
954-
cls._tearDown_class()
954+
pass
955955

956956

957957
class IntegrationTest(PyMongoTestCase):

test/asynchronous/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,11 +949,11 @@ def tearDownClass(cls):
949949

950950
@classmethod
951951
async def _setup_class(cls):
952-
await cls._setup_class()
952+
pass
953953

954954
@classmethod
955955
async def _tearDown_class(cls):
956-
await cls._tearDown_class()
956+
pass
957957

958958

959959
class AsyncIntegrationTest(AsyncPyMongoTestCase):

0 commit comments

Comments
 (0)