Skip to content

Conversation

@d-v-b
Copy link
Collaborator

@d-v-b d-v-b commented Dec 9, 2025

adds methods for creating copies of models with new fields

@d-v-b
Copy link
Collaborator Author

d-v-b commented Dec 9, 2025

from pydantic_zarr.experimental.v3 import ArraySpec, GroupSpec

g = GroupSpec(attributes={"foo": 10}, members={})
a = ArraySpec(
    shape=(10,), 
    attributes={}, 
    data_type='int8',
    codecs=({"name": "bytes"},),
    fill_value=0,
    chunk_key_encoding={"name": "default", "configuration": {"separator": "/"}},
    chunk_grid={"name": "regular", "configuration": {"chunk_shape": (1,)}}
    )

# Copy of g with new members
new_g = g.with_members({"a": a})
print(new_g)
"""
zarr_format=3 attributes={'foo': 10} node_type='group' members={'a': ArraySpec(zarr_format=3, node_type='array', attributes={}, shape=(10,), data_type='int8', chunk_grid={'name': 'regular', 'configuration': {'chunk_shape': (1,)}}, chunk_key_encoding={'name': 'default', 'configuration': {'separator': '/'}}, fill_value=0, codecs=({'name': 'bytes'},), storage_transformers=(), dimension_names=None)}
"""

@d-v-b
Copy link
Collaborator Author

d-v-b commented Dec 9, 2025

worth noting that because these with_* methods trigger validation, you can't easily do this:

a = ArraySpec(shape=(10, 20), ...)
# this will fail validation against the chunk grid
b = a.with_shape((1,2,3))

but if we offered a method that let you update both the chunk grid AND the shape at the same time, then this pattern would work. with_shape_and_chunk_grid is a bit long and I don't have a better idea right now. This is something for a future PR

@d-v-b d-v-b merged commit 157dacd into zarr-developers:main Dec 12, 2025
14 checks passed
@d-v-b d-v-b deleted the feat/with-methods branch December 12, 2025 14:02
@d-v-b d-v-b mentioned this pull request Dec 12, 2025
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