Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: python binding kwargs parsing #5458

Merged
merged 2 commits into from
Dec 26, 2024
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
1 change: 1 addition & 0 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ doc = false

[dependencies]
bytes = "1.5.0"
dict_derive = "0.6.0"
futures = "0.3.28"
# this crate won't be published, we always use the local version
opendal = { version = ">=0", path = "../../core", features = [
Expand Down
9 changes: 7 additions & 2 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ impl Operator {
#[pyo3(signature = (path, bs, **kwargs))]
pub fn write(&self, path: &str, bs: Vec<u8>, kwargs: Option<WriteOptions>) -> PyResult<()> {
let kwargs = kwargs.unwrap_or_default();
let mut write = self.core.write_with(path, bs).append(kwargs.append);
let mut write = self
.core
.write_with(path, bs)
.append(kwargs.append.unwrap_or(false));
if let Some(chunk) = kwargs.chunk {
write = write.chunk(chunk);
}
Expand Down Expand Up @@ -333,7 +336,9 @@ impl AsyncOperator {
let this = self.core.clone();
let bs = bs.as_bytes().to_vec();
future_into_py(py, async move {
let mut write = this.write_with(&path, bs).append(kwargs.append);
let mut write = this
.write_with(&path, bs)
.append(kwargs.append.unwrap_or(false));
if let Some(buffer) = kwargs.chunk {
write = write.chunk(buffer);
}
Expand Down
5 changes: 3 additions & 2 deletions bindings/python/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
// specific language governing permissions and limitations
// under the License.

use pyo3::{pyclass, FromPyObject};
use dict_derive::FromPyObject;
use pyo3::pyclass;

#[pyclass(module = "opendal")]
#[derive(FromPyObject, Default)]
pub struct WriteOptions {
pub append: bool,
pub append: Option<bool>,
pub chunk: Option<usize>,
pub content_type: Option<String>,
pub content_disposition: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_sync_write(service_name, operator, async_operator):
filename = f"test_file_{str(uuid4())}.txt"
content = os.urandom(size)
size = len(content)
operator.write(filename, content)
operator.write(filename, content, content_type="text/plain")
metadata = operator.stat(filename)
assert metadata is not None
assert metadata.mode.is_file()
Expand Down
Loading