Skip to content

Commit 281d1aa

Browse files
committed
fix(asyn): support sync style implementation in async _rm_file
1 parent dae0e80 commit 281d1aa

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
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/implementations/tests/test_local.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -929,35 +929,37 @@ def test_strip_protocol_no_authority(uri, expected, cwd, current_drive):
929929

930930
@pytest.mark.parametrize(
931931
"uri, expected",
932-
[
933-
("file:/path", "/path"),
934-
("file:///path", "/path"),
935-
("file:////path", "//path"),
936-
("local:/path", "/path"),
937-
("s3://bucket/key", "{cwd}/s3://bucket/key"),
938-
("/path", "/path"),
939-
("file:///", "/"),
940-
]
941-
if not WIN
942-
else [
943-
("file:c:/path", "c:/path"),
944-
("file:/c:/path", "c:/path"),
945-
("file:/C:/path", "C:/path"),
946-
("file://c:/path", "c:/path"),
947-
("file:///c:/path", "c:/path"),
948-
("local:/path", "{current_drive}/path"),
949-
("s3://bucket/key", "{cwd}/s3://bucket/key"),
950-
("c:/path", "c:/path"),
951-
("c:\\path", "c:/path"),
952-
("file:///", "{current_drive}/"),
953-
pytest.param(
954-
"file://localhost/c:/path",
955-
"c:/path",
956-
marks=pytest.mark.xfail(
957-
reason="rfc8089 section3 'localhost uri' not supported"
932+
(
933+
[
934+
("file:/path", "/path"),
935+
("file:///path", "/path"),
936+
("file:////path", "//path"),
937+
("local:/path", "/path"),
938+
("s3://bucket/key", "{cwd}/s3://bucket/key"),
939+
("/path", "/path"),
940+
("file:///", "/"),
941+
]
942+
if not WIN
943+
else [
944+
("file:c:/path", "c:/path"),
945+
("file:/c:/path", "c:/path"),
946+
("file:/C:/path", "C:/path"),
947+
("file://c:/path", "c:/path"),
948+
("file:///c:/path", "c:/path"),
949+
("local:/path", "{current_drive}/path"),
950+
("s3://bucket/key", "{cwd}/s3://bucket/key"),
951+
("c:/path", "c:/path"),
952+
("c:\\path", "c:/path"),
953+
("file:///", "{current_drive}/"),
954+
pytest.param(
955+
"file://localhost/c:/path",
956+
"c:/path",
957+
marks=pytest.mark.xfail(
958+
reason="rfc8089 section3 'localhost uri' not supported"
959+
),
958960
),
959-
),
960-
],
961+
]
962+
),
961963
)
962964
def test_strip_protocol_absolute_paths(uri, expected, current_drive, cwd):
963965
expected = expected.format(current_drive=current_drive, cwd=cwd)

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)