Skip to content

Commit

Permalink
fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamman committed May 23, 2024
1 parent fec7adf commit 68b7ac1
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 57 deletions.
88 changes: 61 additions & 27 deletions src/zarr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
from __future__ import annotations

import zarr.codecs # noqa: F401
from zarr._version import version as __version__
from zarr.api.synchronous import (
array,
consolidate_metadata,
copy,
copy_all,
copy_store,
create,
empty,
empty_like,
full,
full_like,
group,
load,
ones,
ones_like,
open,
open_array,
open_consolidated,
open_group,
open_like,
save,
save_array,
save_group,
tree,
zeros,
zeros_like,
)
from zarr.array import Array, AsyncArray
from zarr.config import config # noqa: F401
from zarr.config import config
from zarr.group import AsyncGroup, Group
from zarr.store import (
StoreLike,
make_store_path,
)
from zarr.sync import sync as _sync

# in case setuptools scm screw up and find version to be 0.0.0
assert not __version__.startswith("0.0.0")


async def open_auto_async(store: StoreLike) -> AsyncArray | AsyncGroup:
store_path = make_store_path(store)
try:
return await AsyncArray.open(store_path)
except KeyError:
return await AsyncGroup.open(store_path)


def open_auto(store: StoreLike) -> Array | Group:
object = _sync(
open_auto_async(store),
)
if isinstance(object, AsyncArray):
return Array(object)
if isinstance(object, AsyncGroup):
return Group(object)
raise TypeError(f"Unexpected object type. Got {type(object)}.")
__all__ = [
"__version__",
"config",
"Array",
"AsyncArray",
"Group",
"AsyncGroup",
"tree",
"array",
"consolidate_metadata",
"copy",
"copy_all",
"copy_store",
"create",
"empty",
"empty_like",
"full",
"full_like",
"group",
"load",
"ones",
"ones_like",
"open",
"open_array",
"open_consolidated",
"open_group",
"open_like",
"save",
"save_array",
"save_group",
"zeros",
"zeros_like",
]
Empty file added src/zarr/api/__init__.py
Empty file.
49 changes: 33 additions & 16 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ async def open(
store_path = store_path / path

try:
return await AsyncArray.open(store_path, zarr_format=zarr_format, **kwargs)
return await open_array(store=store_path, zarr_format=zarr_format, **kwargs)
except KeyError:
return await AsyncGroup.open(store_path, zarr_format=zarr_format, **kwargs)
return await open_group(store=store_path, zarr_format=zarr_format, **kwargs)


async def open_consolidated(*args: Any, **kwargs: Any) -> AsyncGroup:
Expand Down Expand Up @@ -298,8 +298,8 @@ async def save_group(
await asyncio.gather(*aws)


# async def tree(*args: Any, **kwargs: Any) -> "TreeViewer":
# raise NotImplementedError
async def tree(*args: Any, **kwargs: Any) -> None:
raise NotImplementedError


async def array(data: npt.ArrayLike, **kwargs: Any) -> AsyncArray:
Expand Down Expand Up @@ -349,12 +349,13 @@ async def group(
store: StoreLike | None = None,
overwrite: bool = False,
chunk_store: StoreLike | None = None, # not used
cache_attrs: bool = True, # not used
cache_attrs: bool | None = None, # not used, default changed
synchronizer: Any | None = None, # not used
path: str | None = None,
zarr_version: ZarrFormat | None = None, # deprecated
zarr_format: ZarrFormat | None = None,
meta_array: Any | None = None, # not used
attributes: dict[str, JSON] | None = None,
) -> AsyncGroup:
"""Create a group.
Expand Down Expand Up @@ -409,28 +410,33 @@ async def group(
if meta_array is not None:
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)

if attributes is None:
attributes = {}

try:
return await AsyncGroup.open(store_path, zarr_format=zarr_format)
except KeyError:
# TODO: pass attributes here
attributes: dict[str, Any] = {}
return await AsyncGroup.open(store=store_path, zarr_format=zarr_format)
except (KeyError, FileNotFoundError):
return await AsyncGroup.create(
store_path, zarr_format=zarr_format, exists_ok=overwrite, attributes=attributes
store=store_path,
zarr_format=zarr_format,
exists_ok=overwrite,
attributes=attributes,
)


async def open_group(
*, # Note: this is a change from v2
store: StoreLike | None = None,
mode: str | None = None, # not used
cache_attrs: bool = True, # not used
cache_attrs: bool | None = None, # not used, default changed
synchronizer: Any = None, # not used
path: str | None = None,
chunk_store: StoreLike | None = None, # not used
storage_options: dict[str, Any] | None = None, # not used
zarr_version: ZarrFormat | None = None, # deprecated
zarr_format: ZarrFormat | None = None,
meta_array: Any | None = None, # not used
attributes: dict[str, JSON] | None = None,
) -> AsyncGroup:
"""Open a group using file-mode-like semantics.
Expand Down Expand Up @@ -481,18 +487,24 @@ async def open_group(
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
if meta_array is not None:
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)

if chunk_store is not None:
warnings.warn("chunk_store is not yet implemented", RuntimeWarning, stacklevel=2)

if storage_options is not None:
warnings.warn("storage_options is not yet implemented", RuntimeWarning, stacklevel=2)

store_path = make_store_path(store)
if path is not None:
store_path = store_path / path

return await AsyncGroup.open(store_path, zarr_format=zarr_format)
if attributes is None:
attributes = {}

try:
return await AsyncGroup.open(store_path, zarr_format=zarr_format)
except (KeyError, FileNotFoundError):
return await AsyncGroup.create(
store_path, zarr_format=zarr_format, exists_ok=True, attributes=attributes
)


# TODO: require kwargs
Expand All @@ -515,7 +527,7 @@ async def create(
read_only: bool | None = None,
object_codec: Codec | None = None, # TODO: type has changed
dimension_separator: Literal[".", "/"] | None = None,
write_empty_chunks: bool = True,
write_empty_chunks: bool = False, # TODO: default has changed
zarr_version: ZarrFormat | None = None, # deprecated
zarr_format: ZarrFormat | None = None,
meta_array: Any | None = None, # TODO: need type
Expand Down Expand Up @@ -637,6 +649,11 @@ async def create(
if zarr_format is None:
zarr_format = 3 # default from config?

if zarr_format == 2 and chunks is None:
chunks = shape
if zarr_format == 3 and chunk_shape is None:
chunk_shape = shape

if order is not None:
warnings.warn(
"order is deprecated, use zarr config instead", DeprecationWarning, stacklevel=2
Expand All @@ -655,7 +672,7 @@ async def create(
warnings.warn("object_codec is not yet implemented", RuntimeWarning, stacklevel=2)
if dimension_separator is not None:
warnings.warn("dimension_separator is not yet implemented", RuntimeWarning, stacklevel=2)
if write_empty_chunks is not None:
if write_empty_chunks:
warnings.warn("write_empty_chunks is not yet implemented", RuntimeWarning, stacklevel=2)
if storage_transformers:
warnings.warn("storage_transformers is not yet implemented", RuntimeWarning, stacklevel=2)
Expand Down
21 changes: 11 additions & 10 deletions src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import zarr.api.asynchronous as async_api
from zarr.array import Array
from zarr.common import ZarrFormat
from zarr.common import JSON, ZarrFormat
from zarr.group import Group
from zarr.store import StoreLike
from zarr.sync import sync
Expand Down Expand Up @@ -210,8 +210,8 @@ def save_group(
"""
return sync(
async_api.save_group(
store=store,
*args, # noqa: B026
store,
*args,
zarr_version=zarr_version,
zarr_format=zarr_format,
path=path,
Expand All @@ -220,9 +220,8 @@ def save_group(
)


# TODO: implement or deprecate
# def tree(*args: Any, **kwargs: Any) -> "TreeViewer":
# return sync(async_api.tree(*args, **kwargs))
def tree(*args: Any, **kwargs: Any) -> None:
return sync(async_api.tree(*args, **kwargs))


# TODO: add type annotations for kwargs
Expand All @@ -235,17 +234,18 @@ def array(data: npt.ArrayLike, **kwargs: Any) -> Array:
return Array(sync(async_api.array(data=data, **kwargs)))


async def group(
def group(
*, # Note: this is a change from v2
store: StoreLike | None = None,
overwrite: bool = False,
chunk_store: StoreLike | None = None, # not used in async_api
cache_attrs: bool = True, # not used in async_api
cache_attrs: bool | None = None, # default changed, not used in async_api
synchronizer: Any | None = None, # not used in async_api
path: str | None = None,
zarr_version: ZarrFormat | None = None, # deprecated
zarr_format: ZarrFormat | None = None,
meta_array: Any | None = None, # not used in async_api
attributes: dict[str, JSON] | None = None,
) -> Group:
"""Create a group.
Expand Down Expand Up @@ -289,6 +289,7 @@ async def group(
zarr_version=zarr_version,
zarr_format=zarr_format,
meta_array=meta_array,
attributes=attributes,
)
)
)
Expand All @@ -298,7 +299,7 @@ def open_group(
*, # Note: this is a change from v2
store: StoreLike | None = None,
mode: str | None = None, # not used in async api
cache_attrs: bool = True, # not used in async api
cache_attrs: bool | None = None, # default changed, not used in async api
synchronizer: Any = None, # not used in async api
path: str | None = None,
chunk_store: StoreLike | None = None, # not used in async api
Expand Down Expand Up @@ -397,7 +398,7 @@ def full(shape: async_api.ShapeLike, fill_value: Any, **kwargs: Any) -> Array:

# TODO: move ArrayLike to common module
# TODO: add type annotations for kwargs
async def full_like(a: async_api.ArrayLike, **kwargs: Any) -> Array:
def full_like(a: async_api.ArrayLike, **kwargs: Any) -> Array:
"""Create a filled array like `a`."""
return Array(sync(async_api.full_like(a, **kwargs)))

Expand Down
2 changes: 2 additions & 0 deletions src/zarr/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def parse_named_configuration(


def parse_shapelike(data: Any) -> tuple[int, ...]:
if isinstance(data, int):
return (data,)
if not isinstance(data, Iterable):
raise TypeError(f"Expected an iterable. Got {data} instead.")
data_tuple = tuple(data)
Expand Down
35 changes: 35 additions & 0 deletions src/zarr/convenience.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import warnings

from zarr.api.synchronous import (
consolidate_metadata,
copy,
copy_all,
copy_store,
load,
open,
open_consolidated,
save,
save_array,
save_group,
tree,
)

warnings.warn(
"zarr.convenience is deprecated, use zarr.api.synchronous",
DeprecationWarning,
stacklevel=2,
)

__all__ = [
"open",
"save_array",
"save_group",
"save",
"load",
"tree",
"copy_store",
"copy",
"copy_all",
"consolidate_metadata",
"open_consolidated",
]
37 changes: 37 additions & 0 deletions src/zarr/creation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import warnings

from zarr.api.synchronous import (
array,
create,
empty,
empty_like,
full,
full_like,
ones,
ones_like,
open_array,
open_like,
zeros,
zeros_like,
)

warnings.warn(
"zarr.creation is deprecated, use zarr.api.synchronous",
DeprecationWarning,
stacklevel=2,
)

__all__ = [
"create",
"empty",
"zeros",
"ones",
"full",
"array",
"open_array",
"empty_like",
"zeros_like",
"ones_like",
"full_like",
"open_like",
]
Loading

0 comments on commit 68b7ac1

Please sign in to comment.