Skip to content

fix: tighten array metadata field validation - #4494

Open
barlowa124 wants to merge 5 commits into
zarr-developers:mainfrom
barlowa124:meta-validate-4453
Open

barlowa124 wants to merge 5 commits into
zarr-developers:mainfrom
barlowa124:meta-validate-4453

Conversation

@barlowa124

@barlowa124 barlowa124 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #4453, fifth checklist item. Four array metadata fields accepted
JSON outside the spec contract.

shape: [true] parsed as (1,) because bool subclasses int.
Document shape fields now reject booleans through
parse_shapelike(..., reject_bool=True) while stored
chunks/chunk_shape keep the lenient default: old versions wrote
true there and repair tests codify reading them. attributes: []
parsed as {}. parse_attributes now requires a dict with string
keys, matching GroupMetadata's existing check.
dimension_names: "xy" parsed as ("x", "y") since a bare string is
iterable. Strings and bytes are excluded before the element check.
storage_transformers: {"name": ...} parsed as a tuple of keys.
mappings are excluded from the iterable check.

Non-Mapping attributes now emit a ZarrDeprecationWarning rather
than raising, since older documents wrote list-valued attributes.
That moved from rejection to deprecation during review. Bool
dimension_names elements are deprecated the same way.

For reviewers

parse_shapelike gains a keyword-only reject_bool flag whose
lenient default preserves the repair path for stored chunk sizes.
attributes: null still parses to {}, matching the group's handling
of missing attributes.

Author attestation

  • I am a human. These are my changes. I have reviewed and understood every change and can explain why each is correct.

TODO

Done:

  • Add unit tests and/or doctests in docstrings

Outstanding:

  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/user-guide/*.md
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

barlowa124 and others added 2 commits October 8, 2026 16:11
Four metadata fields accepted out-of-contract JSON:

- shape accepted booleans ([true] read as (1,)); document shape fields
  now reject them via parse_shapelike(reject_bool=True). Stored
  chunks/chunk_shape fields keep the lenient default since older
  versions wrote true there and repair tests codify reading them.
- attributes accepted a list; it now requires a dict with string keys,
  matching the check GroupMetadata already applies.
- dimension_names accepted a bare string, iterating it into
  characters; strings and bytes are excluded before the element check.
- storage_transformers accepted a dict, iterating it into keys;
  mappings are excluded.

Refs zarr-developers#4453

Generated with Devin
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@github-actions github-actions Bot added needs release notes Automatically applied to PRs which haven't added release notes and removed needs release notes Automatically applied to PRs which haven't added release notes labels Oct 8, 2026
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.72%. Comparing base (069fd20) to head (0a80cf0).
⚠️ Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4494      +/-   ##
==========================================
+ Coverage   94.69%   94.72%   +0.02%     
==========================================
  Files          94       94              
  Lines       13606    13676      +70     
==========================================
+ Hits        12884    12954      +70     
  Misses        722      722              
Files with missing lines Coverage Δ
src/zarr/core/common.py 93.45% <100.00%> (+1.10%) ⬆️
src/zarr/core/metadata/common.py 100.00% <100.00%> (ø)
src/zarr/core/metadata/v3.py 96.91% <100.00%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/zarr/core/metadata/common.py Outdated
if data is None:
return {}

if not isinstance(data, dict) or not all(isinstance(k, str) for k in data):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to check if the input is Mapping, not dict

Comment thread src/zarr/core/metadata/common.py Outdated
if data is None:
return {}

if not isinstance(data, dict) or not all(isinstance(k, str) for k in data):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, this conflicts a bit with the direction in #4400. I think warning first, then issuing a release where we are stricter and raise, is the safer direction

@d-v-b d-v-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The dimension_names string exclusion and the storage_transformers mapping exclusion are correct.
  • In test_array.py, ({"test": ...}) was a dict, not a tuple, so changing it to [...] is a correct fix.

only accepting dicts for attributes is a regression we should avoid, and the parse_shapelike change needs to go via deprecation

Comment thread src/zarr/core/common.py Outdated


def parse_shapelike(data: ShapeLike) -> tuple[int, ...]:
def parse_shapelike(data: ShapeLike, *, reject_bool: bool = False) -> tuple[int, ...]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we shouldn't give a parsing routine a kwarg that increases or decreases leniency. i think its better to deprecate bools, then reject them once the deprecation window has elapsed

- parse_attributes checks Mapping rather than dict and emits
  ZarrDeprecationWarning for out-of-contract input instead of raising;
  dict(data) still runs so accepted values behave exactly as before
- parse_shapelike loses the reject_bool kwarg; bools now warn
  (ZarrDeprecationWarning) wherever they parse, ahead of rejection once
  the deprecation window elapses
- dimension_names string/bytes exclusion and storage_transformers
  mapping exclusion unchanged
- tests updated: bools and non-mapping attributes are covered by
  pytest.warns cases; repair fixtures reading stored true chunks keep
  their assertions
@barlowa124

Copy link
Copy Markdown
Contributor Author

5a4f169 reworks this to deprecation-first. parse_attributes checks Mapping and warns with ZarrDeprecationWarning instead of rejecting, parse_shapelike warns on booleans in all fields ahead of post-window rejection, and the dimension_names/storage_transformers exclusions stay. Repair fixtures that read stored true chunks now assert the warning.

4906534 updates the sharding test that hit the new warning.

This branch has not been deployed

No deployments
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.

2 participants