Skip to content

Commit 76904ea

Browse files
authored
Refactor/narrow array name type (#2499)
* 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
1 parent 3dd04ce commit 76904ea

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/zarr/core/array.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -810,34 +810,30 @@ def path(self) -> str:
810810
return self.store_path.path
811811

812812
@property
813-
def name(self) -> str | None:
813+
def name(self) -> str:
814814
"""Array name following h5py convention.
815815
816816
Returns
817817
-------
818818
str
819819
The name of the array.
820820
"""
821-
if self.path:
822-
# follow h5py convention: add leading slash
823-
name = self.path
824-
if name[0] != "/":
825-
name = "/" + name
826-
return name
827-
return None
821+
# follow h5py convention: add leading slash
822+
name = self.path
823+
if not name.startswith("/"):
824+
name = "/" + name
825+
return name
828826

829827
@property
830-
def basename(self) -> str | None:
828+
def basename(self) -> str:
831829
"""Final component of name.
832830
833831
Returns
834832
-------
835833
str
836834
The basename or final component of the array name.
837835
"""
838-
if self.name is not None:
839-
return self.name.split("/")[-1]
840-
return None
836+
return self.name.split("/")[-1]
841837

842838
@property
843839
def cdata_shape(self) -> ChunkCoords:
@@ -1626,8 +1622,7 @@ def path(self) -> str:
16261622
return self._async_array.path
16271623

16281624
@property
1629-
def name(self) -> str | None:
1630-
"""Array name following h5py convention."""
1625+
def name(self) -> str:
16311626
return self._async_array.name
16321627

16331628
@property

src/zarr/testing/strategies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def arrays(
153153
assert isinstance(a, Array)
154154
if a.metadata.zarr_format == 3:
155155
assert a.fill_value is not None
156+
assert a.name is not None
156157
assert isinstance(root[array_path], Array)
157158
assert nparray.shape == a.shape
158159
assert chunks == a.chunks

tests/test_array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def test_array_name_properties_no_group(
122122
) -> None:
123123
arr = Array.create(store=store, shape=(100,), chunks=(10,), zarr_format=zarr_format, dtype="i4")
124124
assert arr.path == ""
125-
assert arr.name is None
126-
assert arr.basename is None
125+
assert arr.name == "/"
126+
assert arr.basename == ""
127127

128128

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

0 commit comments

Comments
 (0)