Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use Self for typing classmethods
  • Loading branch information
dstansby committed Dec 12, 2024
commit c5efbdf93e6e81beaa255f9c93bb47f3d9af6b31
21 changes: 6 additions & 15 deletions src/pydantic_zarr/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Generic,
Literal,
Mapping,
Self,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -119,9 +120,6 @@ class NodeSpec(StrictBase):
zarr_version: Literal[2] = 2


ArraySpecType = TypeVar("ArraySpecType", bound="ArraySpec")


class ArraySpec(NodeSpec, Generic[TAttr]):
"""
A model of a Zarr Version 2 Array. The specification for the data structure being modeled by
Expand Down Expand Up @@ -182,7 +180,7 @@ def check_ndim(self):

@classmethod
def from_array(
cls: type[ArraySpecType],
cls,
array: npt.NDArray[Any],
chunks: Literal["auto"] | tuple[int, ...] = "auto",
attributes: Literal["auto"] | TAttr = "auto",
Expand All @@ -191,7 +189,7 @@ def from_array(
filters: Literal["auto"] | list[CodecDict] | None = "auto",
dimension_separator: Literal["auto", "/", "."] = "auto",
compressor: Literal["auto"] | CodecDict | None = "auto",
) -> ArraySpecType:
) -> Self:
"""
Create an `ArraySpec` from an array-like object. This is a convenience method for when Zarr array will be modelled from an existing array.
This method takes nearly the same arguments as the `ArraySpec` constructor, minus `shape` and `dtype`, which will be inferred from the `array` argument.
Expand Down Expand Up @@ -289,7 +287,7 @@ def from_array(
)

@classmethod
def from_zarr(cls: type[ArraySpecType], array: zarr.Array) -> ArraySpecType:
def from_zarr(cls, array: zarr.Array) -> Self:
"""
Create an `ArraySpec` from an instance of `zarr.Array`.

Expand Down Expand Up @@ -445,9 +443,6 @@ def like(
return result


GroupSpecType = TypeVar("GroupSpecType", bound="GroupSpec")


class GroupSpec(NodeSpec, Generic[TAttr, TItem]):
"""
A model of a Zarr Version 2 Group.
Expand All @@ -471,9 +466,7 @@ class can be found in the
members: Annotated[dict[str, TItem] | None, AfterValidator(ensure_key_no_path)] = {}

@classmethod
def from_zarr(
cls: type[GroupSpecType], group: zarr.Group, *, depth: int = -1
) -> GroupSpecType:
def from_zarr(cls, group: zarr.Group, *, depth: int = -1) -> Self:
"""
Create a GroupSpec from an instance of `zarr.Group`. Subgroups and arrays contained in the
Zarr group will be converted to instances of `GroupSpec` and `ArraySpec`, respectively,
Expand Down Expand Up @@ -689,9 +682,7 @@ def to_flat(self, root_path: str = ""):
return to_flat(self, root_path=root_path)

@classmethod
def from_flat(
cls: type[GroupSpecType], data: Dict[str, ArraySpec | GroupSpec]
) -> GroupSpecType:
def from_flat(cls, data: Dict[str, ArraySpec | GroupSpec]) -> Self:
"""
Create a `GroupSpec` from a flat hierarchy representation. The flattened hierarchy is a
`dict` with the following constraints: keys must be valid paths; values must
Expand Down
Loading