Skip to content

Commit 1be2543

Browse files
authored
feat(pyo3-object_store): Configurable warning on PyExternalObjectStore creation (#550)
1 parent d4875d3 commit 1be2543

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

pyo3-object_store/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ rust-version = "1.75"
1313
# Include the Python type hints as part of the cargo distribution
1414
include = ["src", "type-hints", "README.md", "LICENSE"]
1515

16+
[features]
17+
default = ["external-store-warning"]
18+
external-store-warning = []
19+
1620
[dependencies]
1721
async-trait = "0.1.85"
1822
bytes = "1"

pyo3-object_store/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ This provides Python builder classes so that Python users can easily create [`Ar
88

99
1. Register the builders.
1010

11-
```rs
12-
#[pymodule]
13-
fn python_module(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
14-
pyo3_object_store::register_store_module(py, m, "python_module", "store")?;
15-
pyo3_object_store::register_exceptions_module(py, m, "python_module", "exceptions")?;
16-
}
17-
```
11+
```rs
12+
#[pymodule]
13+
fn python_module(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
14+
pyo3_object_store::register_store_module(py, m, "python_module", "store")?;
15+
pyo3_object_store::register_exceptions_module(py, m, "python_module", "exceptions")?;
16+
}
17+
```
1818

1919
This exports the underlying Python classes from your own Rust-Python library.
2020

2121
Refer to [`register_store_module`] and [`register_exceptions_module`] for more information.
2222

2323
2. Accept [`PyObjectStore`] as a parameter in your function exported to Python. Its [`into_dyn`][PyObjectStore::into_dyn] method (or `Into` impl) gives you an [`Arc<dyn ObjectStore>`][object_store::ObjectStore].
2424

25-
```rs
26-
#[pyfunction]
27-
pub fn use_object_store(store: PyObjectStore) {
28-
let store: Arc<dyn ObjectStore> = store.into_dyn();
29-
}
30-
```
25+
```rs
26+
#[pyfunction]
27+
pub fn use_object_store(store: PyObjectStore) {
28+
let store: Arc<dyn ObjectStore> = store.into_dyn();
29+
}
30+
```
3131

3232
You can also accept [`AnyObjectStore`] as a parameter, which wraps [`PyObjectStore`] and [`PyExternalObjectStore`]. This allows you to seamlessly recreate `ObjectStore` instances that users pass in from other Python libraries (like [`obstore`][obstore]) that themselves export `pyo3-object_store` builders.
3333

@@ -39,6 +39,10 @@ The [`obstore`][obstore] Python library gives a full real-world example of using
3939

4040
[obstore]: https://developmentseed.org/obstore/latest/
4141

42+
## Feature flags
43+
44+
- `external-store-warning` (enabled by default): Emit a user warning when constructing a `PyExternalObjectStore` (or `AnyObjectStore::PyExternalObjectStore`), to inform users that there may be performance implications due to lack of connection pooling across separately-compiled Python libraries. Disable this feature if you don't want the warning.
45+
4246
## ABI stability
4347

4448
It's [not currently possible](https://github.com/PyO3/pyo3/issues/1444) to share a `#[pyclass]` across multiple Python libraries, except in special cases where the underlying data has a stable ABI.

pyo3-object_store/src/store.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,17 @@ impl<'py> FromPyObject<'py> for PyExternalObjectStore {
206206
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
207207
match ob.extract() {
208208
Ok(inner) => {
209-
let py = ob.py();
209+
#[cfg(feature = "external-store-warning")]
210+
{
211+
let py = ob.py();
210212

211-
let warnings_mod = py.import(intern!(py, "warnings"))?;
212-
let warning = PyRuntimeWarning::new_err(
213+
let warnings_mod = py.import(intern!(py, "warnings"))?;
214+
let warning = PyRuntimeWarning::new_err(
213215
"Successfully reconstructed a store defined in another Python module. Connection pooling will not be shared across store instances.",
214216
);
215-
let args = PyTuple::new(py, vec![warning])?;
216-
warnings_mod.call_method1(intern!(py, "warn"), args)?;
217-
217+
let args = PyTuple::new(py, vec![warning])?;
218+
warnings_mod.call_method1(intern!(py, "warn"), args)?;
219+
}
218220
Ok(Self(inner))
219221
}
220222
Err(err) => Err(err),

0 commit comments

Comments
 (0)