Skip to content

Better exceptions #498

@kushalkolar

Description

@kushalkolar

Hi! I have another suggestion for exception handling. I ran into this:

AttributeError                            Traceback (most recent call last)
Cell In[3], line 5
      1 path = "/wrong_path_by_mistake.ome.zarr"
      3 p = parse_url(path)
----> 5 reader = Reader(p)
      7 nodes = list(reader())
      8 image_node = nodes[0]

File ~/repos/ome-zarr-py/ome_zarr/reader.py:569, in Reader.__init__(self, zarr)
    568 def __init__(self, zarr: ZarrLocation) -> None:
--> 569     assert zarr.exists()
    570     self.zarr = zarr
    571     self.seen: list[ZarrLocation] = [zarr]

AttributeError: 'NoneType' object has no attribute 'exists'

I was wondering why the zarr object is None, but the real issue was that the path I fed in does not exist because of a typo. It would be useful to instead handle whether the path is valid earlier.

By the way, somewhat related an exception should be raised/handled rather than using an assert. As far as I know the standard practice is to use assert only in tests (there are a few reasons, one of them is that there are interpreter options that disable assertions).

Happy to do a PR but thought I'd get your input first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions