Skip to content

Commit

Permalink
Refactor/narrow array name type (#2499)
Browse files Browse the repository at this point in the history
* array name is not nullable

* alter test for nullable array name

* assert a.name is not None

* assert a.name is not None, outside of the conditional
  • Loading branch information
d-v-b authored Nov 22, 2024
1 parent 3dd04ce commit 76904ea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
23 changes: 9 additions & 14 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,34 +810,30 @@ def path(self) -> str:
return self.store_path.path

@property
def name(self) -> str | None:
def name(self) -> str:
"""Array name following h5py convention.
Returns
-------
str
The name of the array.
"""
if self.path:
# follow h5py convention: add leading slash
name = self.path
if name[0] != "/":
name = "/" + name
return name
return None
# follow h5py convention: add leading slash
name = self.path
if not name.startswith("/"):
name = "/" + name
return name

@property
def basename(self) -> str | None:
def basename(self) -> str:
"""Final component of name.
Returns
-------
str
The basename or final component of the array name.
"""
if self.name is not None:
return self.name.split("/")[-1]
return None
return self.name.split("/")[-1]

@property
def cdata_shape(self) -> ChunkCoords:
Expand Down Expand Up @@ -1626,8 +1622,7 @@ def path(self) -> str:
return self._async_array.path

@property
def name(self) -> str | None:
"""Array name following h5py convention."""
def name(self) -> str:
return self._async_array.name

@property
Expand Down
1 change: 1 addition & 0 deletions src/zarr/testing/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def arrays(
assert isinstance(a, Array)
if a.metadata.zarr_format == 3:
assert a.fill_value is not None
assert a.name is not None
assert isinstance(root[array_path], Array)
assert nparray.shape == a.shape
assert chunks == a.chunks
Expand Down
4 changes: 2 additions & 2 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def test_array_name_properties_no_group(
) -> None:
arr = Array.create(store=store, shape=(100,), chunks=(10,), zarr_format=zarr_format, dtype="i4")
assert arr.path == ""
assert arr.name is None
assert arr.basename is None
assert arr.name == "/"
assert arr.basename == ""


@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
Expand Down

0 comments on commit 76904ea

Please sign in to comment.