Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions bindings/python/python/opendal/operator.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ class AsyncOperator:
An awaitable that completes when the directory is created.
"""
def delete(
self, path: builtins.str | os.PathLike | pathlib.Path
self,
path: builtins.str | os.PathLike | pathlib.Path,
*,
version: builtins.str | None = None,
) -> collections.abc.Awaitable[None]:
r"""
Delete a file at the given path.
Expand All @@ -350,6 +353,8 @@ class AsyncOperator:
----------
path : str
The path to the file.
version : str, optional
The version of the file to delete.

Returns
-------
Expand Down Expand Up @@ -2710,7 +2715,12 @@ class Operator:
path : str
The path to the directory.
"""
def delete(self, path: builtins.str | os.PathLike | pathlib.Path) -> None:
def delete(
self,
path: builtins.str | os.PathLike | pathlib.Path,
*,
version: builtins.str | None = None,
) -> None:
r"""
Delete a file at the given path.

Expand All @@ -2722,6 +2732,8 @@ class Operator:
----------
path : str
The path to the file.
version : str, optional
The version of the file to delete.
"""
def exists(self, path: builtins.str | os.PathLike | pathlib.Path) -> builtins.bool:
r"""
Expand Down
30 changes: 23 additions & 7 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,15 @@ impl Operator {
/// ----------
/// path : str
/// The path to the file.
pub fn delete(&self, path: PathBuf) -> PyResult<()> {
/// version : str, optional
/// The version of the file to delete.
#[pyo3(signature = (path, *, version=None))]
pub fn delete(&self, path: PathBuf, version: Option<String>) -> PyResult<()> {
let path = path.to_string_lossy().to_string();
self.core.delete(&path).map_err(format_pyerr)
let opts = DeleteOptions { version };
self.core
.delete_options(&path, opts.into())
.map_err(format_pyerr)
}

/// Check if a path exists.
Expand Down Expand Up @@ -1293,6 +1299,8 @@ impl AsyncOperator {
/// ----------
/// path : str
/// The path to the file.
/// version : str, optional
/// The version of the file to delete.
///
/// Returns
/// -------
Expand All @@ -1302,13 +1310,21 @@ 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>> {
#[pyo3(signature = (path, *, version=None))]
pub fn delete<'p>(
&'p self,
py: Python<'p>,
path: PathBuf,
version: Option<String>,
) -> 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) },
)
let opts = DeleteOptions { version };
future_into_py(py, async move {
this.delete_options(&path, opts.into())
.await
.map_err(format_pyerr)
})
}

/// Check if a path exists.
Expand Down
10 changes: 10 additions & 0 deletions bindings/python/tests/test_async_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
from opendal.exceptions import NotFound


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "delete")
async def test_async_delete_with_version_option(service_name, operator, async_operator):
path = f"random_file_{str(uuid4())}"
await async_operator.write(path, os.urandom(1024))
await async_operator.delete(path, version=None)
with pytest.raises(NotFound):
await async_operator.read(path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "delete", "list", "create_dir")
async def test_async_remove_all(service_name, operator, async_operator):
Expand Down
9 changes: 9 additions & 0 deletions bindings/python/tests/test_sync_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
from opendal.exceptions import NotFound


@pytest.mark.need_capability("read", "write", "delete")
def test_sync_delete_with_version_option(service_name, operator, async_operator):
path = f"random_file_{str(uuid4())}"
operator.write(path, os.urandom(1024))
operator.delete(path, version=None)
with pytest.raises(NotFound):
operator.read(path)


@pytest.mark.need_capability("read", "write", "delete", "list", "create_dir")
def test_sync_remove_all(service_name, operator, async_operator):
parent = f"random_dir_{str(uuid4())}"
Expand Down
Loading