Skip to content

fix: raise a clear TypeError for non-string group keys - #4658

Open
1915rising wants to merge 2 commits into
zarr-developers:mainfrom
1915rising:fix/3387-storepath-key-type-error
Open

1915rising wants to merge 2 commits into
zarr-developers:mainfrom
1915rising:fix/3387-storepath-key-type-error

Conversation

@1915rising

Copy link
Copy Markdown

Summary

Fixes #3387.

Indexing a group with a non-string key used to die deep in the storage layer. On current main the reporter's example gives:

>>> group[0]
TypeError: sequence item 0: expected str instance, int found

That message comes from str.join inside StorePath.__truediv__ and tells the user nothing about what they did wrong (in zarr v2 the same code returned the member named "0"; in 3.1.1 it was a bare AssertionError, which #3392 improved at the old _dereference_path site, but that check was lost when the path code was refactored).

This change makes StorePath.__truediv__ run the appended path through the existing normalize_path — the same validation StorePath.__init__ already applies — so an invalid key raises the codebase's standard, clear error at the point of misuse:

>>> group[0]
TypeError: Object 0 has an invalid type for "path": int

One line changed in src/zarr/storage/_common.py. String keys are unaffected (normalize_path output for a string is what the constructor would apply to the joined result anyway), and pathlib.Path / bytes operands now behave the same way they do when passed to StorePath directly.

I chose a clear TypeError over silently converting int keys to strings: group member names are strings, and accepting 0 for "0" would hide bugs in calling code. Happy to change direction if maintainers would rather restore the v2 conversion.

For reviewers

The behaviour change to look at is only the error for non-string keys; the valid-key path is unchanged. Regression coverage is in test_group_getitem and test_asyncgroup_getitem, asserting the new TypeError for group[0].

Tests: the issue's repro before/after as above; tests/test_group.py, tests/test_store/test_core.py, tests/test_store/test_utils.py and tests/test_api.py — 1662 passed, 226 skipped (optional-dependency skips), 0 failures after the change; the same targeted getitem tests passed before the change (18 passed) with the repro failing as shown.

Disclosure: this change was prepared with AI coding assistance, in line with the project's AI-assisted contributions guidance; every line has been reviewed and the repro and test runs above were executed against this exact code.

TODO

  • Add unit tests and/or doctests in docstrings
  • Changes documented as a new file in changes/

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.91%. Comparing base (593d394) to head (c25f908).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4658   +/-   ##
=======================================
  Coverage   94.91%   94.91%           
=======================================
  Files          96       96           
  Lines       14070    14070           
=======================================
+ Hits        13354    13355    +1     
+ Misses        716      715    -1     
Files with missing lines Coverage Δ
src/zarr/storage/_common.py 93.85% <100.00%> (ø)

... and 1 file 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.

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.

_dereference_path assertion error

1 participant