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

rename exists_ok to overwrite #2548

Merged
merged 2 commits into from
Dec 12, 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
18 changes: 9 additions & 9 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
_OVERWRITE_MODES: tuple[AccessModeLiteral, ...] = ("a", "r+", "w")


def _infer_exists_ok(mode: AccessModeLiteral) -> bool:
def _infer_overwrite(mode: AccessModeLiteral) -> bool:
"""
Check that an ``AccessModeLiteral`` is compatible with overwriting an existing Zarr node.
"""
Expand Down Expand Up @@ -414,14 +414,14 @@ async def save_array(
arr = np.array(arr)
shape = arr.shape
chunks = getattr(arr, "chunks", None) # for array-likes with chunks attribute
exists_ok = kwargs.pop("exists_ok", None) or _infer_exists_ok(mode)
overwrite = kwargs.pop("overwrite", None) or _infer_overwrite(mode)
new = await AsyncArray.create(
store_path,
zarr_format=zarr_format,
shape=shape,
dtype=arr.dtype,
chunks=chunks,
exists_ok=exists_ok,
overwrite=overwrite,
**kwargs,
)
await new.setitem(slice(None), arr)
Expand Down Expand Up @@ -647,7 +647,7 @@ async def group(
return await AsyncGroup.from_store(
store=store_path,
zarr_format=_zarr_format,
exists_ok=overwrite,
overwrite=overwrite,
attributes=attributes,
)

Expand Down Expand Up @@ -753,12 +753,12 @@ async def open_group(
except (KeyError, FileNotFoundError):
pass
if mode in _CREATE_MODES:
exists_ok = _infer_exists_ok(mode)
overwrite = _infer_overwrite(mode)
_zarr_format = zarr_format or _default_zarr_version()
return await AsyncGroup.from_store(
store_path,
zarr_format=_zarr_format,
exists_ok=exists_ok,
overwrite=overwrite,
attributes=attributes,
)
raise FileNotFoundError(f"Unable to find group: {store_path}")
Expand Down Expand Up @@ -933,7 +933,7 @@ async def create(
dtype=dtype,
compressor=compressor,
fill_value=fill_value,
exists_ok=overwrite,
overwrite=overwrite,
filters=filters,
dimension_separator=dimension_separator,
zarr_format=zarr_format,
Expand Down Expand Up @@ -1120,12 +1120,12 @@ async def open_array(
return await AsyncArray.open(store_path, zarr_format=zarr_format)
except FileNotFoundError:
if not store_path.read_only and mode in _CREATE_MODES:
exists_ok = _infer_exists_ok(mode)
overwrite = _infer_overwrite(mode)
_zarr_format = zarr_format or _default_zarr_version()
return await create(
store=store_path,
zarr_format=_zarr_format,
overwrite=exists_ok,
overwrite=overwrite,
**kwargs,
)
raise
Expand Down
30 changes: 15 additions & 15 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ async def create(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> AsyncArray[ArrayV2Metadata]: ...

Expand Down Expand Up @@ -294,7 +294,7 @@ async def create(
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
dimension_names: Iterable[str] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> AsyncArray[ArrayV3Metadata]: ...

Expand Down Expand Up @@ -322,7 +322,7 @@ async def create(
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
dimension_names: Iterable[str] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> AsyncArray[ArrayV3Metadata]: ...

Expand Down Expand Up @@ -355,7 +355,7 @@ async def create(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]: ...

Expand Down Expand Up @@ -387,7 +387,7 @@ async def create(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
"""
Expand Down Expand Up @@ -429,7 +429,7 @@ async def create(
compressor : dict[str, JSON], optional
The compressor used to compress the data (default is None).
V2 only. V3 arrays should not have 'compressor' parameter.
exists_ok : bool, optional
overwrite : bool, optional
Whether to raise an error if the store already exists (default is False).
data : npt.ArrayLike, optional
The data to be inserted into the array (default is None).
Expand Down Expand Up @@ -489,7 +489,7 @@ async def create(
codecs=codecs,
dimension_names=dimension_names,
attributes=attributes,
exists_ok=exists_ok,
overwrite=overwrite,
order=order,
)
elif zarr_format == 2:
Expand Down Expand Up @@ -522,7 +522,7 @@ async def create(
filters=filters,
compressor=compressor,
attributes=attributes,
exists_ok=exists_ok,
overwrite=overwrite,
)
else:
raise ValueError(f"Insupported zarr_format. Got: {zarr_format}")
Expand Down Expand Up @@ -552,9 +552,9 @@ async def _create_v3(
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
dimension_names: Iterable[str] | None = None,
attributes: dict[str, JSON] | None = None,
exists_ok: bool = False,
overwrite: bool = False,
) -> AsyncArray[ArrayV3Metadata]:
if exists_ok:
if overwrite:
if store_path.store.supports_deletes:
await store_path.delete_dir()
else:
Expand Down Expand Up @@ -609,9 +609,9 @@ async def _create_v2(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
attributes: dict[str, JSON] | None = None,
exists_ok: bool = False,
overwrite: bool = False,
) -> AsyncArray[ArrayV2Metadata]:
if exists_ok:
if overwrite:
if store_path.store.supports_deletes:
await store_path.delete_dir()
else:
Expand Down Expand Up @@ -1463,7 +1463,7 @@ def create(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
) -> Array:
"""Creates a new Array instance from an initialized store.

Expand Down Expand Up @@ -1493,7 +1493,7 @@ def create(
The filters used to compress the data (default is None).
compressor : dict[str, JSON], optional
The compressor used to compress the data (default is None).
exists_ok : bool, optional
overwrite : bool, optional
Whether to raise an error if the store already exists (default is False).

Returns
Expand All @@ -1518,7 +1518,7 @@ def create(
order=order,
filters=filters,
compressor=compressor,
exists_ok=exists_ok,
overwrite=overwrite,
),
)
return cls(async_array)
Expand Down
40 changes: 20 additions & 20 deletions src/zarr/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ async def from_store(
store: StoreLike,
*,
attributes: dict[str, Any] | None = None,
exists_ok: bool = False,
overwrite: bool = False,
zarr_format: ZarrFormat = 3,
) -> AsyncGroup:
store_path = await make_store_path(store)

if exists_ok:
if overwrite:
if store_path.store.supports_deletes:
await store_path.delete_dir()
else:
Expand Down Expand Up @@ -629,7 +629,7 @@ async def setitem(self, key: str, value: Any) -> None:
"""
path = self.store_path / key
await async_api.save_array(
store=path, arr=value, zarr_format=self.metadata.zarr_format, exists_ok=True
store=path, arr=value, zarr_format=self.metadata.zarr_format, overwrite=True
)

async def getitem(
Expand Down Expand Up @@ -919,7 +919,7 @@ async def create_group(
self,
name: str,
*,
exists_ok: bool = False,
overwrite: bool = False,
attributes: dict[str, Any] | None = None,
) -> AsyncGroup:
"""Create a sub-group.
Expand All @@ -928,7 +928,7 @@ async def create_group(
----------
name : str
Group name.
exists_ok : bool, optional
overwrite : bool, optional
If True, do not raise an error if the group already exists.
attributes : dict, optional
Group attributes.
Expand All @@ -941,7 +941,7 @@ async def create_group(
return await type(self).from_store(
self.store_path / name,
attributes=attributes,
exists_ok=exists_ok,
overwrite=overwrite,
zarr_format=self.metadata.zarr_format,
)

Expand All @@ -960,8 +960,8 @@ async def require_group(self, name: str, overwrite: bool = False) -> AsyncGroup:
g : AsyncGroup
"""
if overwrite:
# TODO: check that exists_ok=True errors if an array exists where the group is being created
grp = await self.create_group(name, exists_ok=True)
# TODO: check that overwrite=True errors if an array exists where the group is being created
grp = await self.create_group(name, overwrite=True)
else:
try:
item: (
Expand Down Expand Up @@ -1018,7 +1018,7 @@ async def create_array(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
"""
Expand Down Expand Up @@ -1052,7 +1052,7 @@ async def create_array(
Filters for the array.
compressor : dict[str, JSON] | None = None
The compressor for the array.
exists_ok : bool = False
overwrite : bool = False
If True, a pre-existing array or group at the path of this array will
be overwritten. If False, the presence of a pre-existing array or group is
an error.
Expand All @@ -1077,7 +1077,7 @@ async def create_array(
order=order,
filters=filters,
compressor=compressor,
exists_ok=exists_ok,
overwrite=overwrite,
zarr_format=self.metadata.zarr_format,
data=data,
)
Expand Down Expand Up @@ -1651,7 +1651,7 @@ def from_store(
*,
attributes: dict[str, Any] | None = None,
zarr_format: ZarrFormat = 3,
exists_ok: bool = False,
overwrite: bool = False,
) -> Group:
"""Instantiate a group from an initialized store.

Expand All @@ -1663,7 +1663,7 @@ def from_store(
A dictionary of JSON-serializable values with user-defined attributes.
zarr_format : {2, 3}, optional
Zarr storage format version.
exists_ok : bool, optional
overwrite : bool, optional
If True, do not raise an error if the group already exists.

Returns
Expand All @@ -1680,7 +1680,7 @@ def from_store(
AsyncGroup.from_store(
store,
attributes=attributes,
exists_ok=exists_ok,
overwrite=overwrite,
zarr_format=zarr_format,
),
)
Expand Down Expand Up @@ -2217,7 +2217,7 @@ def create_array(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> Array:
"""Create a zarr array within this AsyncGroup.
Expand Down Expand Up @@ -2251,7 +2251,7 @@ def create_array(
Filters for the array.
compressor : dict[str, JSON] | None = None
The compressor for the array.
exists_ok : bool = False
overwrite : bool = False
If True, a pre-existing array or group at the path of this array will
be overwritten. If False, the presence of a pre-existing array or group is
an error.
Expand Down Expand Up @@ -2280,7 +2280,7 @@ def create_array(
order=order,
filters=filters,
compressor=compressor,
exists_ok=exists_ok,
overwrite=overwrite,
data=data,
)
)
Expand Down Expand Up @@ -2558,7 +2558,7 @@ def array(
filters: list[dict[str, JSON]] | None = None,
compressor: dict[str, JSON] | None = None,
# runtime
exists_ok: bool = False,
overwrite: bool = False,
data: npt.ArrayLike | None = None,
) -> Array:
"""Create a zarr array within this AsyncGroup.
Expand Down Expand Up @@ -2592,7 +2592,7 @@ def array(
Filters for the array.
compressor : dict[str, JSON] | None = None
The compressor for the array.
exists_ok : bool = False
overwrite : bool = False
If True, a pre-existing array or group at the path of this array will
be overwritten. If False, the presence of a pre-existing array or group is
an error.
Expand Down Expand Up @@ -2622,7 +2622,7 @@ def array(
order=order,
filters=filters,
compressor=compressor,
exists_ok=exists_ok,
overwrite=overwrite,
data=data,
)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def async_group(request: pytest.FixtureRequest, tmpdir: LEGACY_PATH) -> As
store,
attributes=param.attributes,
zarr_format=param.zarr_format,
exists_ok=False,
overwrite=False,
)


Expand Down
Loading
Loading