Skip to content

Commit d4bd113

Browse files
committed
Disallow Any generics for mypy
1 parent 2795339 commit d4bd113

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ docs = [
3535
"mkdocstrings[python]",
3636
"pytest-examples",
3737
"pydantic==2.11",
38-
"zarr>=3.1.0"
38+
"zarr>=3.1.0",
3939
]
4040

4141
[tool.hatch]
@@ -167,7 +167,6 @@ plugins = "pydantic.mypy"
167167
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
168168
strict = true
169169
# TODO: remove each of these and fix any errors:
170-
disallow_any_generics = false
171170
warn_return_any = false
172171

173172
[tool.pytest.ini_options]

src/pydantic_zarr/v2.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Self,
1212
TypeAlias,
1313
TypeVar,
14-
Union,
1514
cast,
1615
overload,
1716
)
@@ -39,14 +38,17 @@
3938
if TYPE_CHECKING:
4039
from zarr.abc.store import Store
4140

42-
TBaseAttr: TypeAlias = Mapping[str, object] | BaseModel
43-
TBaseItem: TypeAlias = Union["GroupSpec", "ArraySpec"]
41+
TBaseAttr: TypeAlias = "Mapping[str, Any] | BaseModel"
42+
TBaseItem: TypeAlias = "GroupSpec[Any, Any] | ArraySpec[Any]"
4443

44+
# These types are for convenience when dealing with unknown ArraySpecs and GroupSpecs
45+
# because type variables don't have default values
4546
AnyArraySpec: TypeAlias = "ArraySpec[Any]"
4647
AnyGroupSpec: TypeAlias = "GroupSpec[Any, Any]"
48+
AnyNodeSpec: TypeAlias = "AnyArraySpec | AnyGroupSpec"
4749

48-
TAttr = TypeVar("TAttr", bound=TBaseAttr)
49-
TItem = TypeVar("TItem", bound=TBaseItem)
50+
TAttr = TypeVar("TAttr", bound="TBaseAttr")
51+
TItem = TypeVar("TItem", bound="TBaseItem")
5052

5153
DtypeStr = Annotated[str, BeforeValidator(stringify_dtype)]
5254

@@ -398,7 +400,7 @@ def to_zarr(
398400

399401
def like(
400402
self,
401-
other: ArraySpec | zarr.Array,
403+
other: AnyArraySpec | zarr.Array,
402404
*,
403405
include: IncEx = None,
404406
exclude: IncEx = None,
@@ -447,7 +449,7 @@ def like(
447449
True
448450
"""
449451

450-
other_parsed: ArraySpec
452+
other_parsed: AnyArraySpec
451453
if isinstance(other, zarr.Array):
452454
other_parsed = ArraySpec.from_zarr(other)
453455
else:
@@ -601,7 +603,7 @@ def to_zarr(
601603

602604
def like(
603605
self,
604-
other: GroupSpec | zarr.Group,
606+
other: AnyGroupSpec | zarr.Group,
605607
include: IncEx = None,
606608
exclude: IncEx = None,
607609
) -> bool:
@@ -652,15 +654,15 @@ def like(
652654
True
653655
"""
654656

655-
other_parsed: GroupSpec
657+
other_parsed: AnyGroupSpec
656658
if isinstance(other, zarr.Group):
657659
other_parsed = GroupSpec.from_zarr(other)
658660
else:
659661
other_parsed = other
660662

661663
return model_like(self, other_parsed, include=include, exclude=exclude)
662664

663-
def to_flat(self, root_path: str = "") -> dict[str, AnyArraySpec | AnyGroupSpec]:
665+
def to_flat(self, root_path: str = "") -> dict[str, AnyNodeSpec]:
664666
"""
665667
Flatten this `GroupSpec`.
666668
This method returns a `dict` with string keys and values that are `GroupSpec` or
@@ -878,7 +880,7 @@ def to_flat(
878880
return dict(sorted(result.items(), key=lambda v: len(v[0])))
879881

880882

881-
def from_flat(data: dict[str, ArraySpec | GroupSpec]) -> ArraySpec | GroupSpec:
883+
def from_flat(data: dict[str, AnyNodeSpec]) -> AnyNodeSpec:
882884
"""
883885
Wraps `from_flat_group`, handling the special case where a Zarr array is defined at the root of
884886
a hierarchy and thus is not contained by a Zarr group.

src/pydantic_zarr/v3.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
Self,
1414
TypeAlias,
1515
TypeVar,
16-
Union,
1716
cast,
1817
overload,
1918
)
@@ -45,12 +44,13 @@
4544

4645

4746
TBaseAttr: TypeAlias = Mapping[str, object] | BaseModel
48-
TBaseItem: TypeAlias = Union["GroupSpec", "ArraySpec"]
47+
TBaseItem: TypeAlias = "GroupSpec[Any, Any] | ArraySpec[Any]"
4948

5049
# These types are for convenience when dealing with unknown ArraySpecs and GroupSpecs
5150
# because type variables don't have default values
52-
AnyArraySpec: TypeAlias = "ArraySpec[TBaseAttr]"
53-
AnyGroupSpec: TypeAlias = "GroupSpec[TBaseAttr, TBaseItem]"
51+
AnyArraySpec: TypeAlias = "ArraySpec[Any]"
52+
AnyGroupSpec: TypeAlias = "GroupSpec[Any, Any]"
53+
AnyNodeSpec: TypeAlias = "AnyArraySpec | AnyGroupSpec"
5454

5555
TAttr = TypeVar("TAttr", bound=TBaseAttr)
5656
TItem = TypeVar("TItem", bound=TBaseItem)
@@ -438,7 +438,7 @@ class GroupSpec(NodeSpec, Generic[TAttr, TItem]):
438438
members: Annotated[Mapping[str, TItem] | None, AfterValidator(ensure_key_no_path)] = {}
439439

440440
@classmethod
441-
def from_flat(cls, data: Mapping[str, AnyArraySpec | AnyGroupSpec]) -> Self:
441+
def from_flat(cls, data: Mapping[str, AnyNodeSpec]) -> Self:
442442
"""
443443
Create a `GroupSpec` from a flat hierarchy representation.
444444
@@ -490,7 +490,7 @@ def from_flat(cls, data: Mapping[str, AnyArraySpec | AnyGroupSpec]) -> Self:
490490
from_flated = from_flat_group(data)
491491
return cls(**from_flated.model_dump())
492492

493-
def to_flat(self, root_path: str = "") -> dict[str, AnyArraySpec | AnyGroupSpec]:
493+
def to_flat(self, root_path: str = "") -> dict[str, AnyNodeSpec]:
494494
"""
495495
Flatten this `GroupSpec`.
496496
This method returns a `dict` with string keys and values that are `GroupSpec` or
@@ -527,7 +527,7 @@ def to_flat(self, root_path: str = "") -> dict[str, AnyArraySpec | AnyGroupSpec]
527527
return to_flat(self, root_path=root_path)
528528

529529
@classmethod
530-
def from_zarr(cls, group: zarr.Group, *, depth: int = -1) -> GroupSpec[TAttr, TItem]:
530+
def from_zarr(cls, group: zarr.Group, *, depth: int = -1) -> AnyGroupSpec:
531531
"""
532532
Create a GroupSpec from a zarr group. Subgroups and arrays contained in the zarr
533533
group will be converted to instances of GroupSpec and ArraySpec, respectively,
@@ -712,7 +712,7 @@ def from_zarr(element: zarr.Array, *, depth: int = ...) -> AnyArraySpec: ...
712712
def from_zarr(element: zarr.Group, *, depth: int = ...) -> AnyGroupSpec: ...
713713

714714

715-
def from_zarr(element: zarr.Array | zarr.Group, *, depth: int = -1) -> AnyArraySpec | AnyGroupSpec:
715+
def from_zarr(element: zarr.Array | zarr.Group, *, depth: int = -1) -> AnyNodeSpec:
716716
"""
717717
Recursively parse a Zarr group or Zarr array into an ArraySpec or GroupSpec.
718718
@@ -755,7 +755,7 @@ def to_zarr(
755755

756756

757757
def to_zarr(
758-
spec: AnyArraySpec | AnyGroupSpec,
758+
spec: AnyNodeSpec,
759759
store: Store,
760760
path: str,
761761
overwrite: bool = False,
@@ -787,7 +787,7 @@ def to_zarr(
787787

788788

789789
def from_flat(
790-
data: Mapping[str, AnyArraySpec | AnyGroupSpec],
790+
data: Mapping[str, AnyNodeSpec],
791791
) -> AnyArraySpec | AnyGroupSpec:
792792
"""
793793
Wraps `from_flat_group`, handling the special case where a Zarr array is defined at the root of
@@ -830,7 +830,7 @@ def from_flat(
830830

831831

832832
def from_flat_group(
833-
data: Mapping[str, AnyArraySpec | AnyGroupSpec],
833+
data: Mapping[str, AnyNodeSpec],
834834
) -> AnyGroupSpec:
835835
"""
836836
Generate a `GroupSpec` from a flat representation of a hierarchy, i.e. a `dict` with
@@ -962,7 +962,7 @@ def auto_dimension_names(data: object) -> tuple[str | None, ...] | None:
962962
return None
963963

964964

965-
def to_flat(node: ArraySpec | GroupSpec, root_path: str = "") -> dict[str, ArraySpec | GroupSpec]:
965+
def to_flat(node: AnyNodeSpec, root_path: str = "") -> dict[str, AnyNodeSpec]:
966966
"""
967967
Flatten a `GroupSpec` or `ArraySpec`.
968968
Converts a `GroupSpec` or `ArraySpec` and a string, into a `dict` with string keys and
@@ -1002,7 +1002,7 @@ def to_flat(node: ArraySpec | GroupSpec, root_path: str = "") -> dict[str, Array
10021002
{'/g1': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None), '': GroupSpec(zarr_format=2, attributes={'foo': 'bar'}, members=None)}
10031003
"""
10041004
result = {}
1005-
model_copy: AnyArraySpec | AnyGroupSpec
1005+
model_copy: AnyNodeSpec
10061006
if isinstance(node, ArraySpec):
10071007
model_copy = node.model_copy(deep=True)
10081008
else:

0 commit comments

Comments
 (0)