Copilot commented on code in PR #7877: URL: https://github.com/apache/opendal/pull/7877#discussion_r3535568418
########## bindings/python/src/layers/retry.rs: ########## @@ -0,0 +1,103 @@ +// 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. + +use std::time::Duration; + +use super::Layer; +use super::PythonLayer; +use crate::*; +use opendal::Operator; + +/// A layer that retries operations that fail with temporary errors. +/// +/// Operations are retried if they fail with an error for which +/// `Error.is_temporary` returns `True`. If all retries are exhausted, +/// the error is marked as persistent and then returned. +/// +/// Notes +/// ----- +/// After an operation on a `Reader` or `Writer` has failed through +/// all retries, the object is in an undefined state. Reusing it +/// can lead to exceptions. +#[pyclass(module = "opendal.layers.retry", extends=Layer, skip_from_py_object)] +#[derive(Clone)] +pub struct RetryLayer(ocore::layers::RetryLayer); + +impl PythonLayer for RetryLayer { + fn layer(&self, op: Operator) -> Operator { + op.layer(self.0.clone()) + } +} +#[pymethods] +impl RetryLayer { + /// Create a new RetryLayer. + /// + /// Parameters + /// ---------- + /// max_times : Optional[int] + /// Maximum number of retry attempts. Defaults to ``3``. + /// factor : Optional[float] + /// Backoff factor applied between retries. Defaults to ``2.0``. + /// jitter : bool + /// Whether to apply jitter to the backoff. Defaults to ``False``. + /// max_delay : Optional[float] + /// Maximum delay (in seconds) between retries. Defaults to ``60.0``. + /// min_delay : Optional[float] + /// Minimum delay (in seconds) between retries. Defaults to ``1.0``. + /// + /// Returns + /// ------- + /// RetryLayer + #[new] + #[pyo3(signature = ( + max_times = None, + factor = None, + jitter = false, + max_delay = None, + min_delay = None + ))] + fn new( + max_times: Option<usize>, + factor: Option<f32>, + jitter: bool, + max_delay: Option<f64>, + min_delay: Option<f64>, + ) -> PyResult<PyClassInitializer<Self>> { + let mut retry = ocore::layers::RetryLayer::default(); + if let Some(max_times) = max_times { + retry = retry.with_max_times(max_times); + } + if let Some(factor) = factor { + retry = retry.with_factor(factor); + } + if jitter { + retry = retry.with_jitter(); + } + if let Some(max_delay) = max_delay { + retry = retry.with_max_delay(Duration::from_micros((max_delay * 1_000_000.0) as u64)); + } Review Comment: `max_delay` is converted from `f64` to `Duration` via float arithmetic and an `as u64` cast, which silently clamps NaN/negative values (and truncates fractions) instead of reporting invalid input. This can lead to surprising behavior for Python callers (e.g., `max_delay=-1.0` becoming `0`). Consider validating that the value is finite and non-negative and using `Duration::from_secs_f64` for the conversion. ########## bindings/python/src/layers/retry.rs: ########## @@ -0,0 +1,103 @@ +// 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. + +use std::time::Duration; + +use super::Layer; +use super::PythonLayer; +use crate::*; +use opendal::Operator; + +/// A layer that retries operations that fail with temporary errors. +/// +/// Operations are retried if they fail with an error for which +/// `Error.is_temporary` returns `True`. If all retries are exhausted, +/// the error is marked as persistent and then returned. +/// +/// Notes +/// ----- +/// After an operation on a `Reader` or `Writer` has failed through +/// all retries, the object is in an undefined state. Reusing it +/// can lead to exceptions. +#[pyclass(module = "opendal.layers.retry", extends=Layer, skip_from_py_object)] +#[derive(Clone)] +pub struct RetryLayer(ocore::layers::RetryLayer); + +impl PythonLayer for RetryLayer { + fn layer(&self, op: Operator) -> Operator { + op.layer(self.0.clone()) + } +} +#[pymethods] +impl RetryLayer { + /// Create a new RetryLayer. + /// + /// Parameters + /// ---------- + /// max_times : Optional[int] + /// Maximum number of retry attempts. Defaults to ``3``. + /// factor : Optional[float] + /// Backoff factor applied between retries. Defaults to ``2.0``. + /// jitter : bool + /// Whether to apply jitter to the backoff. Defaults to ``False``. + /// max_delay : Optional[float] + /// Maximum delay (in seconds) between retries. Defaults to ``60.0``. + /// min_delay : Optional[float] + /// Minimum delay (in seconds) between retries. Defaults to ``1.0``. + /// + /// Returns + /// ------- + /// RetryLayer + #[new] + #[pyo3(signature = ( + max_times = None, + factor = None, + jitter = false, + max_delay = None, + min_delay = None + ))] + fn new( + max_times: Option<usize>, + factor: Option<f32>, + jitter: bool, + max_delay: Option<f64>, + min_delay: Option<f64>, + ) -> PyResult<PyClassInitializer<Self>> { + let mut retry = ocore::layers::RetryLayer::default(); + if let Some(max_times) = max_times { + retry = retry.with_max_times(max_times); + } + if let Some(factor) = factor { + retry = retry.with_factor(factor); + } + if jitter { + retry = retry.with_jitter(); + } + if let Some(max_delay) = max_delay { + retry = retry.with_max_delay(Duration::from_micros((max_delay * 1_000_000.0) as u64)); + } + if let Some(min_delay) = min_delay { + retry = retry.with_min_delay(Duration::from_micros((min_delay * 1_000_000.0) as u64)); + } Review Comment: `min_delay` is converted from `f64` to `Duration` via float arithmetic and an `as u64` cast, which silently clamps NaN/negative values (and truncates fractions) instead of reporting invalid input. Validating the value (finite + non-negative) avoids surprising behavior for Python callers. ########## bindings/python/src/lib.rs: ########## @@ -83,6 +83,35 @@ mod _opendal { #[pymodule] mod layers { + use pyo3::prelude::*; + + // Canonical per-layer submodules: `opendal.layers.<name>.<Layer>`. + #[pymodule] + mod capability_override { Review Comment: This PR introduces canonical per-layer submodules (e.g. `opendal.layers.retry`) as a new supported import surface. Current Python tests only exercise the flat re-exports (`opendal.layers.RetryLayer`, etc.), so regressions in `import opendal.layers.<name>` / `from opendal.layers.<name> import <Layer>` could slip through. Add a small Python test that imports each canonical submodule and asserts it exposes the same class object as the flat re-export. ########## bindings/python/scripts/postprocess_stubs.py: ########## @@ -84,8 +95,101 @@ def fix_incomplete_exceptions() -> None: path.write_text(EXCEPTIONS_STUB) +def relocate_layers_package() -> None: + """Relocate the generated layers stubs to the ``opendal/layers/`` package. + + PyO3 emits the ``layers`` module's stubs under ``GEN/_opendal/``: an + ``__init__.pyi`` that redefines every layer class (plus the base ``Layer``) + and one ``<name>.pyi`` per canonical ``opendal.layers.<name>`` submodule that + *also* redefines its layer class. + + To avoid declaring each layer twice (which makes type checkers treat the flat + and canonical paths as distinct types), the canonical definition is kept in + the per-layer submodule stub and ``__init__.pyi`` is rewritten to re-export + from it. The base ``Layer`` stays defined in ``__init__.pyi``. Each submodule + stub redefines its own class with a ``... as <Class>2`` self-import that only + exists to type the ``__new__`` return; that alias is stripped so the class + references itself directly. + """ + dst = PKG / "layers" + dst.mkdir(exist_ok=True) + Review Comment: `relocate_layers_package()` writes the newly generated per-layer stubs into `python/opendal/layers/`, but it doesn't remove stale `*.pyi` files from prior runs. If a layer is renamed/removed, old stub files can linger and confuse type checkers. Consider clearing existing `*.pyi` files in the destination (except `__init__.pyi`) before writing the new ones. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
