Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/narrow array name type #2499

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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