Repository navigation
IO: Delete only the exact file path #4512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -725,6 +725,13 @@ def delete(self, location: str | InputFile | OutputFile) -> None: | |
| scheme, netloc, path = self.parse_location(str_location, self.properties) | ||
| fs = self.fs_by_scheme(scheme, netloc) | ||
|
|
||
| if not _is_local_path(str_location): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| parsed = urlparse(str_location) | ||
| if parsed.query: | ||
| path = f"{path}?{parsed.query}" | ||
| if parsed.fragment: | ||
| path = f"{path}#{parsed.fragment}" | ||
|
|
||
|
Comment on lines
+729
to
+734
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try: | ||
| fs.delete_file(path) | ||
| except FileNotFoundError: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,30 @@ def test_fsspec_local_fs_can_create_path_without_parent_dir(fsspec_fileio: Fsspe | |
| pytest.fail("Failed to write to file without parent directory") | ||
|
|
||
|
|
||
| def test_fsspec_delete_does_not_expand_glob(fsspec_fileio: FsspecFileIO) -> None: | ||
| """Test delete removes only the named file when the name contains glob characters""" | ||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| for name in ("data-1.parquet", "data-2.parquet", "data-[12].parquet"): | ||
| with open(f"{tmpdirname}/{name}", "wb") as f: | ||
| f.write(b"foo") | ||
|
|
||
| fsspec_fileio.delete(f"{tmpdirname}/data-[12].parquet") | ||
|
|
||
| assert sorted(os.listdir(tmpdirname)) == ["data-1.parquet", "data-2.parquet"] | ||
|
|
||
|
|
||
| def test_fsspec_delete_preserves_query_and_fragment(fsspec_fileio: FsspecFileIO) -> None: | ||
| """Test delete passes the location to rm_file without parsing its query or fragment""" | ||
| fs = mock.Mock(spec=AbstractFileSystem) | ||
| location = "s3://bucket/data.parquet?versionId=1#fragment" | ||
|
|
||
| with mock.patch.object(fsspec_fileio, "_get_fs_from_uri", return_value=fs): | ||
| fsspec_fileio.delete(location) | ||
|
|
||
| fs.rm_file.assert_called_once_with(location) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| fs.rm.assert_not_called() | ||
|
|
||
|
|
||
| def test_fsspec_get_fs_instance_per_thread_caching(fsspec_fileio: FsspecFileIO) -> None: | ||
| """Test that filesystem instances are cached per-thread by `FsspecFileIO.get_fs`""" | ||
| fs_instances: list[AbstractFileSystem] = [] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -407,6 +407,18 @@ def test_deleting_hdfs_file_not_found() -> None: | |
| assert "Cannot delete file, does not exist:" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_deleting_s3_file_preserves_query_and_fragment() -> None: | ||
| """Test delete keeps a query or fragment as part of the object key instead of dropping it""" | ||
| for name in ("data.parquet?versionId=1", "data.parquet#1"): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: could you use |
||
| s3fs_mock = MagicMock() | ||
|
|
||
| with patch.object(PyArrowFileIO, "_initialize_fs") as submocked: | ||
| submocked.return_value = s3fs_mock | ||
| PyArrowFileIO().delete(f"s3://foo/{name}") | ||
|
|
||
| s3fs_mock.delete_file.assert_called_once_with(f"foo/{name}") | ||
|
|
||
|
|
||
| def test_pyarrow_s3_session_properties() -> None: | ||
| session_properties: Properties = { | ||
| "s3.endpoint": "http://localhost:9000", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the glob expansion, but not
?/#. s3fssplit_pathdoeskeypart.partition("?versionId=")regardless ofversion_aware, sorm_file("s3://b/data.parquet?versionId=1")still deletesdata.parquet. gcsfs (whenversion_aware) treats a numeric#or?generation=as a generation, and adlfs parses?versionid=.Also a small behavior change:
rmraisedFileNotFoundErrorfor a missing path throughexpand_path, whilerm_fileon S3 is a silentdelete_object. Probably fine for the callers, but worth noting.