Skip to content

Commit

Permalink
Chagne key errors to use python builtins, add simple test for handling (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci authored Oct 31, 2024
1 parent 2c9a311 commit 22acd16
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 deletions.
3 changes: 1 addition & 2 deletions icechunk-python/python/icechunk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from zarr.core.sync import SyncMixin

from ._icechunk_python import (
KeyNotFound,
PyIcechunkStore,
S3Credentials,
SnapshotMetadata,
Expand Down Expand Up @@ -485,7 +484,7 @@ async def get(

try:
result = await self._store.get(key, byte_range)
except KeyNotFound as _e:
except KeyError as _e:
# Zarr python expects None to be returned if the key does not exist
# but an IcechunkStore returns an error if the key does not exist
return None
Expand Down
6 changes: 0 additions & 6 deletions icechunk-python/python/icechunk/_icechunk_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,6 @@ class VirtualRefConfig:
"""
...

class KeyNotFound(Exception):
def __init__(
self,
info: Any
): ...

class StoreConfig:
"""Configuration for an IcechunkStore"""

Expand Down
24 changes: 8 additions & 16 deletions icechunk-python/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use icechunk::{
format::IcechunkFormatError, repository::RepositoryError, zarr::StoreError,
};
use pyo3::{
exceptions::{PyException, PyValueError},
exceptions::{PyKeyError, PyValueError},
PyErr,
};
use thiserror::Error;
Expand All @@ -16,17 +16,17 @@ use thiserror::Error;
#[derive(Debug, Error)]
#[allow(dead_code)]
pub(crate) enum PyIcechunkStoreError {
#[error("key not found error: {0}")]
KeyNotFound(String),
#[error("store error: {0}")]
StoreError(#[from] StoreError),
#[error("repository Error: {0}")]
#[error("repository error: {0}")]
RepositoryError(#[from] RepositoryError),
#[error("icechunk format error: {0}")]
IcechunkFormatError(#[from] IcechunkFormatError),
#[error("value error: {0}")]
#[error("{0}")]
PyKeyError(String),
#[error("{0}")]
PyValueError(String),
#[error("error: {0}")]
#[error("{0}")]
PyError(#[from] PyErr),
#[error("{0}")]
UnkownError(String),
Expand All @@ -35,20 +35,12 @@ pub(crate) enum PyIcechunkStoreError {
impl From<PyIcechunkStoreError> for PyErr {
fn from(error: PyIcechunkStoreError) -> Self {
match error {
PyIcechunkStoreError::KeyNotFound(_) => {
KeyNotFound::new_err(error.to_string())
}
PyIcechunkStoreError::PyKeyError(e) => PyKeyError::new_err(e),
PyIcechunkStoreError::PyValueError(e) => PyValueError::new_err(e),
PyIcechunkStoreError::PyError(err) => err,
_ => PyValueError::new_err(error.to_string()),
}
}
}

pub(crate) type PyIcechunkStoreResult<T> = Result<T, PyIcechunkStoreError>;

pyo3::create_exception!(
_icechunk_python,
KeyNotFound,
PyException,
"The key is not present in the repository"
);
11 changes: 4 additions & 7 deletions icechunk-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use icechunk::{
Repository, SnapshotMetadata,
};
use pyo3::{
exceptions::PyValueError,
exceptions::{PyKeyError, PyValueError},
prelude::*,
types::{PyBytes, PyList, PyNone, PyString},
};
Expand All @@ -32,8 +32,6 @@ use tokio::{
sync::{Mutex, RwLock},
};

pub use errors::KeyNotFound;

#[pyclass]
struct PyIcechunkStore {
consolidated: ConsolidatedStore,
Expand Down Expand Up @@ -674,7 +672,7 @@ impl PyIcechunkStore {
let byte_range = byte_range.unwrap_or((None, None)).into();
let data = store.read().await.get(&key, &byte_range).await;
// We need to distinguish the "safe" case of trying to fetch an uninitialized key
// from other types of errors, we use KeyNotFound exception for that
// from other types of errors, we use PyKeyError exception for that
match data {
Ok(data) => {
let pybytes = Python::with_gil(|py| {
Expand All @@ -683,7 +681,7 @@ impl PyIcechunkStore {
});
Ok(pybytes)
}
Err(StoreError::NotFound(_)) => Err(KeyNotFound::new_err(key)),
Err(StoreError::NotFound(_)) => Err(PyKeyError::new_err(key)),
Err(err) => Err(PyIcechunkStoreError::StoreError(err).into()),
}
})
Expand Down Expand Up @@ -1045,9 +1043,8 @@ async fn do_set_virtual_ref(

/// The icechunk Python module implemented in Rust.
#[pymodule]
fn _icechunk_python(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
fn _icechunk_python(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
m.add("KeyNotFound", py.get_type_bound::<KeyNotFound>())?;
m.add_class::<PyStorageConfig>()?;
m.add_class::<PyIcechunkStore>()?;
m.add_class::<PyS3Credentials>()?;
Expand Down
5 changes: 5 additions & 0 deletions icechunk-python/tests/test_timetravel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import icechunk
import zarr
import zarr.core
import zarr.core.buffer


def test_timetravel():
Expand Down Expand Up @@ -83,3 +85,6 @@ async def test_branch_reset():
assert "a/zarr.json" in keys
assert "b/zarr.json" not in keys

assert (
await store.get("b/zarr.json", zarr.core.buffer.default_buffer_prototype())
) is None

0 comments on commit 22acd16

Please sign in to comment.