Skip to content

Fix NameError in v3 from_zarr/ArraySpec.like and KeyError in model_dump#169

Merged
d-v-b merged 4 commits into
zarr-developers:mainfrom
d-v-b:fix/v3-zarr-nameerror
Jun 19, 2026
Merged

Fix NameError in v3 from_zarr/ArraySpec.like and KeyError in model_dump#169
d-v-b merged 4 commits into
zarr-developers:mainfrom
d-v-b:fix/v3-zarr-nameerror

Conversation

@d-v-b

@d-v-b d-v-b commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes four verified runtime crashes in pydantic_zarr.v3 and pydantic_zarr.experimental.v3, identified in #165.

Bug 1: ArraySpec.like raises NameError when called with a spec or zarr array

ArraySpec.like in src/pydantic_zarr/v3.py did a bare isinstance(other, zarr.Array) but zarr is only imported under TYPE_CHECKING, so at runtime this crashes:

from pydantic_zarr.v3 import ArraySpec
spec = ArraySpec(...)
spec.like(spec)  # NameError: name 'zarr' is not defined

Fix: Use the sys.modules guard that GroupSpec.like already uses:

if (zarr := sys.modules.get("zarr")) and isinstance(other, zarr.Array):

Bug 2: Module-level from_zarr in v3.py raises NameError

The function body does isinstance(element, zarr.Array) with no runtime import:

from pydantic_zarr.v3 import from_zarr
import zarr
arr = zarr.open_array(...)
from_zarr(arr)  # NameError: name 'zarr' is not defined

Fix: Add import zarr inside the function body, matching v2.py.

Bug 3: Module-level from_zarr in experimental/v3.py raises NameError

Same as Bug 2 but in the experimental module.

Fix: Add import zarr inside the function body.

Bug 4: ArraySpec.model_dump(exclude={'dimension_names'}) raises KeyError

The model_dump override in both v3.py and experimental/v3.py did d["dimension_names"] unconditionally. When the field is excluded, the key is absent:

spec.model_dump(exclude={"dimension_names"})  # KeyError: 'dimension_names'

Fix: Use d.get("dimension_names") is None and d.pop("dimension_names", None).

Additional fixes

  • conftest.py zarr 3.2 compatibility: zarr 3.2 renamed Structured to Struct (with different constructor signature); the test conftest crashed on import with zarr 3.2. Updated to detect whichever name is present in the dtype registry.
  • mypy python_version: Bumped from "3.11" to "3.12" so mypy can parse zarr 3.2 code that uses the PEP 695 type statement.

Test plan

  • test_arrayspec_like_spec_vs_specArraySpec.like(spec) no longer raises NameError
  • test_arrayspec_like_spec_vs_zarr_arrayArraySpec.like(zarr_array) no longer raises NameError
  • test_from_zarr_array / test_from_zarr_group — module-level from_zarr no longer raises NameError
  • test_model_dump_exclude_dimension_namesmodel_dump(exclude={'dimension_names'}) no longer raises KeyError
  • Same four tests repeated for experimental.v3
  • All 10 new regression tests pass; 19,435 existing tests unchanged

🤖 Generated with Claude Code

d-v-b and others added 3 commits June 14, 2026 22:52
- ArraySpec.like in v3.py used bare `isinstance(other, zarr.Array)` with zarr
  only imported under TYPE_CHECKING, causing NameError at runtime; switch to
  the sys.modules guard already used by GroupSpec.like.
- Module-level from_zarr in v3.py and experimental/v3.py referenced zarr.Array
  with no runtime import; add `import zarr` inside the function body (mirrors v2.py).
- ArraySpec.model_dump in both v3.py and experimental/v3.py did
  d["dimension_names"] unconditionally, raising KeyError when dimension_names
  was excluded via model_dump(exclude={...}); use d.get() + pop(key, None).
- Fix conftest.py for zarr 3.2 compatibility: Structured was renamed to Struct
  and requires fields; detect whichever name is present in the registry.
- Bump mypy python_version from 3.11 to 3.12 so mypy can parse zarr 3.2
  which uses the PEP 695 `type` statement syntax.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@d-v-b d-v-b merged commit a5b6c5e into zarr-developers:main Jun 19, 2026
15 checks passed
@d-v-b d-v-b deleted the fix/v3-zarr-nameerror branch June 19, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant