From bddd24fafcad1f10aa6b0d5eb56bd4dd03e51f55 Mon Sep 17 00:00:00 2001 From: Andy He Date: Wed, 25 Nov 2026 23:09:16 -0500 Subject: [PATCH] fix(fsspec): don't silently skip `get_file` for `file://` and Windows paths `_get_file` returned without writing anything when `lpath` had a URL scheme. That includes `file://` URLs, and every Windows path, since `urlparse("C:\\...")` reads the drive letter as a scheme. Use fsspec's `split_protocol`, which ignores drive letters, strip a `file://` prefix, and raise a `ValueError` for any other protocol. --- obstore/python/obstore/fsspec.py | 13 +++++++++--- tests/test_fsspec.py | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/obstore/python/obstore/fsspec.py b/obstore/python/obstore/fsspec.py index 7ffbcc2a..560f7d97 100644 --- a/obstore/python/obstore/fsspec.py +++ b/obstore/python/obstore/fsspec.py @@ -43,6 +43,7 @@ import fsspec.asyn import fsspec.spec +from fsspec.core import split_protocol from fsspec.implementations.local import make_path_posix from obstore import open_reader, open_writer @@ -485,9 +486,15 @@ async def _put_file( await store.put_async(rpath, Path(lpath)) async def _get_file(self, rpath: str, lpath: str, **_kwargs: Any) -> None: - res = urlparse(lpath) - if res.scheme or await self._local_store._isdir(lpath): # noqa: SLF001 - # lpath need to be local file and cannot contain scheme + # lpath needs to be a local path. `split_protocol` does not treat a Windows + # drive letter (e.g. `C:`) as a protocol. + protocol, local_path = split_protocol(lpath) + if protocol not in (None, "file"): + err_msg = f"Expected a local path for lpath, got {lpath}" + raise ValueError(err_msg) + lpath = local_path + + if await self._local_store._isdir(lpath): # noqa: SLF001 return resp = await self._cat_file(rpath) diff --git a/tests/test_fsspec.py b/tests/test_fsspec.py index 6767ca46..f8634cfa 100644 --- a/tests/test_fsspec.py +++ b/tests/test_fsspec.py @@ -588,6 +588,41 @@ async def test_put_files_async( await fs._rm(remote_file_path) +def test_get_file_to_file_url(): + register("memory") + fs: FsspecStore = fsspec.filesystem("memory") + fs.pipe_file("get_file_src.txt", b"hello world") + + with TemporaryDirectory() as tmp: + local_file_path = Path(tmp) / "out.txt" + fs.get_file("get_file_src.txt", local_file_path.as_uri()) + assert local_file_path.read_bytes() == b"hello world" + + +def test_get_file_to_windows_path(): + register("memory") + fs: FsspecStore = fsspec.filesystem("memory") + lpath = r"C:\Users\me\out.txt" + + with ( + patch.object(fs, "_cat_file", return_value=b"hello world"), + patch.object(fs._local_store, "_isdir", return_value=False), + patch.object(fs._local_store, "_pipe_file") as mock_pipe_file, + ): + fs.get_file("get_file_src.txt", lpath) + + mock_pipe_file.assert_called_once_with(lpath, b"hello world") + + +def test_get_file_to_non_local_url_raises(): + register("memory") + fs: FsspecStore = fsspec.filesystem("memory") + fs.pipe_file("get_file_src.txt", b"hello world") + + with pytest.raises(ValueError, match="local path"): + fs.get_file("get_file_src.txt", "s3://bucket/out.txt") + + @pytest.mark.network def test_remote_parquet(minio_bucket: tuple[S3Config, ClientConfig]): register(["https", "s3"])