Skip to content

Commit

Permalink
Fix error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Dec 20, 2023
1 parent fbd2d84 commit f0a5dea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
31 changes: 18 additions & 13 deletions anndata/_io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import h5py
from packaging.version import Version

from anndata.compat import H5Group, ZarrGroup, add_note
from anndata.compat import H5Array, H5Group, ZarrArray, ZarrGroup, add_note

from .._core.sparse_dataset import BaseCompressedSparseDataset

Expand Down Expand Up @@ -151,31 +151,36 @@ class AnnDataReadError(OSError):
pass


def _get_parent(elem):
def _get_path(
elem: ZarrGroup | ZarrArray | H5Group | H5Array | BaseCompressedSparseDataset,
) -> str:
try:
import zarr
except ImportError:
zarr = None
if zarr and isinstance(elem, (zarr.Group, zarr.Array)):
parent = elem.store # Not sure how to always get a name out of this
elif isinstance(elem, BaseCompressedSparseDataset):
parent = elem.group.file.name
path = elem.path
if isinstance(elem, BaseCompressedSparseDataset):
path = elem.group.name
else:
parent = elem.file.name
return parent
path = elem.name
return f'/{path.removeprefix("/")}'


def add_key_note(e: BaseException, elem, key, op=Literal["read", "writ"]) -> None:
def add_key_note(
e: BaseException,
elem: ZarrGroup | ZarrArray | H5Group | H5Array | BaseCompressedSparseDataset,
key: str,
op: Literal["read", "writ"],
) -> None:
if any(
f"Error raised while {op}ing key" in note
for note in getattr(e, "__notes__", [])
):
return
parent = _get_parent(elem)
add_note(
e,
f"Error raised while {op}ing key {key!r} of {type(elem)} to " f"{parent}",
)

msg = f"Error raised while {op}ing key {key!r} of {type(elem)} to {_get_path(elem)}"
add_note(e, msg)


def report_read_key_on_error(func):
Expand Down
3 changes: 2 additions & 1 deletion anndata/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ def check_error_or_notes_match(e: pytest.ExceptionInfo, pattern: str | re.Patter
import traceback

message = "".join(traceback.format_exception_only(e.type, e.value))
assert re.search(
# To match pytest semantics, this must be `match`, not `search`.
assert re.match(
pattern, message
), f"Could not find pattern: '{pattern}' in error:\n\n{message}\n"

Expand Down
4 changes: 3 additions & 1 deletion anndata/tests/test_io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def test_write_error_info(diskfmt, tmp_path):
# Assuming we don't define a writer for tuples
a = ad.AnnData(uns={"a": {"b": {"c": (1, 2, 3)}}})

with pytest_8_raises(IORegistryError, match=r"Error raised while writing key 'c'"):
with pytest_8_raises(
IORegistryError, match=r"Error raised while writing key 'c'.*to /uns/a/b"
):
write(a)


Expand Down

0 comments on commit f0a5dea

Please sign in to comment.