Skip to content
Merged
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
12 changes: 8 additions & 4 deletions bindings/python/python/opendal/operator.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class AsyncOperator:
Operator
"""

def __new__(cls, /, scheme: str | Scheme, **kwargs: str) -> AsyncOperator:
def __new__(
cls, /, scheme: str | Scheme, **kwargs: str | PathLike[str]
) -> AsyncOperator:
"""
Create a new `AsyncOperator`.

Expand All @@ -51,7 +53,7 @@ class AsyncOperator:
scheme : str | Scheme
The scheme of the service.
**kwargs : dict
The options for the service.
The options for the service. Path-like values are converted to strings.

Returns
-------
Expand Down Expand Up @@ -722,7 +724,9 @@ class Operator:
AsyncOperator
"""

def __new__(cls, /, scheme: str | Scheme, **kwargs: str) -> Operator:
def __new__(
cls, /, scheme: str | Scheme, **kwargs: str | PathLike[str]
) -> Operator:
"""
Create a new blocking `Operator`.

Expand All @@ -731,7 +735,7 @@ class Operator:
scheme : str | Scheme
The scheme of the service.
**kwargs : dict
The options for the service.
The options for the service. Path-like values are converted to strings.

Returns
-------
Expand Down
50 changes: 42 additions & 8 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,18 @@ impl Operator {
/// scheme : str | Scheme
/// The scheme of the service.
/// **kwargs : dict
/// The options for the service.
/// The options for the service. Path-like values are converted to strings.
///
/// Returns
/// -------
/// Operator
/// The new operator.
#[new]
#[pyo3(signature = (scheme: "str | Scheme", *, **kwargs: "str"))]
pub fn new(scheme: Bound<PyAny>, kwargs: Option<HashMap<String, String>>) -> PyResult<Self> {
#[pyo3(signature = (scheme: "str | Scheme", *, **kwargs: "str | PathLike[str]"))]
pub fn new(
scheme: Bound<PyAny>,
kwargs: Option<HashMap<String, Bound<PyAny>>>,
) -> PyResult<Self> {
let scheme = if let Ok(scheme_str) = scheme.extract::<&str>() {
scheme_str.to_string()
} else if let Ok(py_scheme) = scheme.extract::<Scheme>() {
Expand All @@ -200,7 +203,21 @@ impl Operator {
));
};
let scheme = normalize_scheme(&scheme);
let map = kwargs.unwrap_or_default();
let map = kwargs
.unwrap_or_default()
.into_iter()
.map(|(key, value)| {
let value = value.extract::<String>().or_else(|_| {
value
.py()
.import("os")?
.getattr("fspath")?
.call1((&value,))?
.extract::<String>()
})?;
Ok((key, value))
})
.collect::<PyResult<HashMap<_, _>>>()?;

Ok(Operator {
core: build_blocking_operator(&scheme, map.clone())?,
Expand Down Expand Up @@ -947,15 +964,18 @@ impl AsyncOperator {
/// scheme : str | Scheme
/// The scheme of the service.
/// **kwargs : dict
/// The options for the service.
/// The options for the service. Path-like values are converted to strings.
///
/// Returns
/// -------
/// AsyncOperator
/// The new async operator.
#[new]
#[pyo3(signature = (scheme: "str | Scheme", * ,**kwargs: "str"))]
pub fn new(scheme: Bound<PyAny>, kwargs: Option<HashMap<String, String>>) -> PyResult<Self> {
#[pyo3(signature = (scheme: "str | Scheme", *, **kwargs: "str | PathLike[str]"))]
pub fn new(
scheme: Bound<PyAny>,
kwargs: Option<HashMap<String, Bound<PyAny>>>,
) -> PyResult<Self> {
let scheme = if let Ok(scheme_str) = scheme.extract::<&str>() {
scheme_str.to_string()
} else if let Ok(py_scheme) = scheme.extract::<Scheme>() {
Expand All @@ -967,7 +987,21 @@ impl AsyncOperator {
};
let scheme = normalize_scheme(&scheme);

let map = kwargs.unwrap_or_default();
let map = kwargs
.unwrap_or_default()
.into_iter()
.map(|(key, value)| {
let value = value.extract::<String>().or_else(|_| {
value
.py()
.import("os")?
.getattr("fspath")?
.call1((&value,))?
.extract::<String>()
})?;
Ok((key, value))
})
.collect::<PyResult<HashMap<_, _>>>()?;

Ok(AsyncOperator {
core: build_operator(&scheme, map.clone())?,
Expand Down
46 changes: 46 additions & 0 deletions bindings/python/tests/test_operator_construction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.

import pytest

import opendal


@pytest.mark.parametrize("path_like", [False, True])
def test_operator_new_with_path(tmp_path, path_like):
root = tmp_path if path_like else str(tmp_path)
op = opendal.Operator("fs", root=root)
op.write("constructor.txt", b"sync constructor")
assert op.read("constructor.txt") == b"sync constructor"
assert (tmp_path / "constructor.txt").read_bytes() == b"sync constructor"


@pytest.mark.asyncio
@pytest.mark.parametrize("path_like", [False, True])
async def test_async_operator_new_with_path(tmp_path, path_like):
root = tmp_path if path_like else str(tmp_path)
op = opendal.AsyncOperator("fs", root=root)
await op.write("constructor.txt", b"async constructor")
assert await op.read("constructor.txt") == b"async constructor"
assert (tmp_path / "constructor.txt").read_bytes() == b"async constructor"


@pytest.mark.parametrize("operator_type", [opendal.Operator, opendal.AsyncOperator])
@pytest.mark.parametrize("root", [True, b"bytes-root"])
def test_constructor_rejects_non_string_root(operator_type, root):
with pytest.raises(TypeError):
operator_type("fs", root=root)
Loading