Skip to content

Commit 97c0a0f

Browse files
authored
fix(asyn): support sync style implementation in async _rm_file (#1951)
* fix(asyn): support sync style implementation in async _rm_file * lint
1 parent 0d9a922 commit 97c0a0f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

fsspec/asyn.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ def loop(self):
328328
return self._loop
329329

330330
async def _rm_file(self, path, **kwargs):
331+
if (
332+
inspect.iscoroutinefunction(self._rm)
333+
and type(self)._rm is not AsyncFileSystem._rm
334+
):
335+
return await self._rm(path, recursive=False, batch_size=1, **kwargs)
331336
raise NotImplementedError
332337

333338
async def _rm(self, path, recursive=False, batch_size=None, **kwargs):

fsspec/tests/test_async.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,42 @@ async def test_async_streamed_file_read():
205205
== b"foo-bar" * 20
206206
)
207207
await streamed_file.close()
208+
209+
210+
def test_rm_file_with_rm_implementation():
211+
class AsyncFSWithRm(fsspec.asyn.AsyncFileSystem):
212+
def __init__(self, **kwargs):
213+
super().__init__(**kwargs)
214+
self.removed_paths = []
215+
216+
async def _rm(self, path, recursive=False, batch_size=None, **kwargs):
217+
if isinstance(path, str):
218+
path = [path]
219+
for p in path:
220+
self.removed_paths.append(p)
221+
return None
222+
223+
fs = AsyncFSWithRm()
224+
fs.rm_file("test/file.txt")
225+
assert "test/file.txt" in fs.removed_paths
226+
227+
228+
def test_rm_file_with_rm_file_implementation():
229+
class AsyncFSWithRmFile(fsspec.asyn.AsyncFileSystem):
230+
def __init__(self, **kwargs):
231+
super().__init__(**kwargs)
232+
self.removed_paths = []
233+
234+
async def _rm_file(self, path, **kwargs):
235+
self.removed_paths.append(path)
236+
return None
237+
238+
fs = AsyncFSWithRmFile()
239+
fs.rm_file("test/file.txt")
240+
assert "test/file.txt" in fs.removed_paths
241+
242+
243+
def test_rm_file_without_implementation():
244+
fs = fsspec.asyn.AsyncFileSystem()
245+
with pytest.raises(NotImplementedError):
246+
fs.rm_file("test/file.txt")

0 commit comments

Comments
 (0)