You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by te...@apache.org on 2026/04/27 18:26:33 UTC

(opendal) 01/02: feat(bindings/python): expose version and recursive options for delete

This is an automated email from the ASF dual-hosted git repository.

TennyZhuang pushed a commit to branch python-delete-recursive
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit e5955b2b0fdbc28f29bd041e6808f5bcc2dd47c9
Author: tianyizhuang <ti...@tianyizhuangs-Mac-mini.local>
AuthorDate: Tue Apr 28 02:20:13 2026 +0800

    feat(bindings/python): expose version and recursive options for delete
    
    Adds optional `version` and `recursive` parameters to both sync and async
    `delete` methods, enabling:
    - Deletion of specific file versions on version-aware backends (S3 with versioning)
    - Recursive directory deletion via the delete endpoint
    
    Updates `DeleteOptions` struct to include `recursive` field and adds
    `FromPyObject` impl for proper kwarg extraction.
    
    Includes focused behavior tests verifying the new parameters are accepted.
    
    Co-Authored-By: Claude Sonnet 4.6 <no...@anthropic.com>
---
 bindings/python/src/operator.rs              | 28 +++++++++++---
 bindings/python/src/options.rs               | 16 +++++++-
 bindings/python/tests/test_delete_options.py | 57 ++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs
index abf1bd71b..e542fe78f 100644
--- a/bindings/python/src/operator.rs
+++ b/bindings/python/src/operator.rs
@@ -1302,13 +1302,31 @@ impl AsyncOperator {
         type_repr="collections.abc.Awaitable[None]",
         imports=("collections.abc")
     ))]
-    pub fn delete<'p>(&'p self, py: Python<'p>, path: PathBuf) -> PyResult<Bound<'p, PyAny>> {
+    /// version : str, optional
+    ///     The version of the file to delete. Only supported on version-aware backends.
+    /// recursive : bool, optional
+    ///     If True, delete the path recursively. Only supported on backends that support recursive delete.
+    #[pyo3(signature = (path, *, version=None, recursive=None))]
+    pub fn delete<'p>(
+        &'p self,
+        py: Python<'p>,
+        path: PathBuf,
+        version: Option<String>,
+        recursive: Option<bool>,
+    ) -> PyResult<Bound<'p, PyAny>> {
         let this = self.core.clone();
         let path = path.to_string_lossy().to_string();
-        future_into_py(
-            py,
-            async move { this.delete(&path).await.map_err(format_pyerr) },
-        )
+        future_into_py(py, async move {
+            if version.is_some() || recursive.is_some() {
+                let opts = ocore::options::DeleteOptions {
+                    version,
+                    recursive: recursive.unwrap_or(false),
+                };
+                this.delete_options(&path, opts).await.map_err(format_pyerr)
+            } else {
+                this.delete(&path).await.map_err(format_pyerr)
+            }
+        })
     }
 
     /// Check if a path exists.
diff --git a/bindings/python/src/options.rs b/bindings/python/src/options.rs
index 914628898..64c2bbb52 100644
--- a/bindings/python/src/options.rs
+++ b/bindings/python/src/options.rs
@@ -286,13 +286,27 @@ impl From<StatOptions> for ocore::options::StatOptions {
 #[derive(Default, Debug)]
 pub struct DeleteOptions {
     pub version: Option<String>,
+    pub recursive: Option<bool>,
+}
+
+impl<'a, 'py> FromPyObject<'a, 'py> for DeleteOptions {
+    type Error = PyErr;
+
+    fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
+        let dict = downcast_kwargs(obj)?;
+
+        Ok(Self {
+            version: extract_optional(&dict, "version")?,
+            recursive: extract_optional(&dict, "recursive")?,
+        })
+    }
 }
 
 impl From<DeleteOptions> for ocore::options::DeleteOptions {
     fn from(opts: DeleteOptions) -> Self {
         Self {
             version: opts.version,
-            ..Default::default()
+            recursive: opts.recursive.unwrap_or(false),
         }
     }
 }
diff --git a/bindings/python/tests/test_delete_options.py b/bindings/python/tests/test_delete_options.py
new file mode 100644
index 000000000..140141556
--- /dev/null
+++ b/bindings/python/tests/test_delete_options.py
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from uuid import uuid4
+
+import pytest
+
+
+@pytest.mark.need_capability("write", "delete")
+def test_delete_accepts_version_param(service_name, operator, async_operator):
+    path = f"test_delete_version_{uuid4()}.txt"
+    operator.write(path, b"test content")
+    operator.delete(path, version="v1")
+
+
+@pytest.mark.need_capability("write", "delete")
+def test_delete_accepts_recursive_param(service_name, operator, async_operator):
+    path = f"test_delete_recursive_{uuid4()}.txt"
+    operator.write(path, b"test content")
+    operator.delete(path, recursive=True)
+
+
+@pytest.mark.need_capability("write", "delete")
+def test_delete_default_params_unchanged(service_name, operator, async_operator):
+    path = f"test_delete_default_{uuid4()}.txt"
+    operator.write(path, b"test content")
+    operator.delete(path)
+
+
+@pytest.mark.need_capability("write", "delete")
+@pytest.mark.asyncio
+async def test_async_delete_accepts_version_param(service_name, operator, async_operator):
+    path = f"test_async_delete_version_{uuid4()}.txt"
+    await async_operator.write(path, b"test content")
+    await async_operator.delete(path, version="v1")
+
+
+@pytest.mark.need_capability("write", "delete")
+@pytest.mark.asyncio
+async def test_async_delete_accepts_recursive_param(service_name, operator, async_operator):
+    path = f"test_async_delete_recursive_{uuid4()}.txt"
+    await async_operator.write(path, b"test content")
+    await async_operator.delete(path, recursive=True)