Conversation
|
for context, we have a |
| return dict(data) # type: ignore[arg-type] | ||
|
|
||
|
|
||
| class ArrayMetadataJSON_V3(TypedDict, extra_items=AllowedExtraField): # type: ignore[call-arg] |
There was a problem hiding this comment.
this should eventually be a public type
| ) | ||
|
|
||
|
|
||
| def check_array_metadata_like(data: object) -> ArrayMetadataJSONLike_V3: |
There was a problem hiding this comment.
I would like this function to eventually be public
| return cast(ArrayMetadataJSONLike_V3, data) | ||
|
|
||
|
|
||
| class ArrayMetadataJSONLike_V3(TypedDict, extra_items=AllowedExtraField): # type: ignore[call-arg] |
There was a problem hiding this comment.
this should eventually be public
| shape: ShapeLike, | ||
| data_type: ZDTypeLike, |
There was a problem hiding this comment.
metadata now parses shapes and data types.
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Expect[TIn, TOut]: |
There was a problem hiding this comment.
I am liking this pattern for simple test cast definition. we could consider using this elsewhere in the codebase.
|
This is ready for another round of review. If this pattern looks good for array metadata, I would start extending it to all our metadata classes. The end result would be a pair of public types for each metadata class: the generous input JSON-like type, and the strict output JSON-like type, and functions that parse unknown values into both types, or error. I think this would be very useful for libraries that want type-safe routines for working with array metadata documents. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3799 +/- ##
==========================================
- Coverage 93.11% 93.08% -0.03%
==========================================
Files 85 86 +1
Lines 11365 11370 +5
==========================================
+ Hits 10582 10584 +2
- Misses 783 786 +3
🚀 New features to boost your workflow:
|
|
@d-v-b I'm trying to make my way through a backlog of review requests, but I think it'd help to have the merge conflicts resolved on this first |
Introduces a generic `JSONSerializable[T_co]` protocol in `zarr.abc.serializable` parameterized on the JSON output type. Adds a `to_json` method to `ArrayV3Metadata` that returns an `ArrayMetadataJSON_V3` payload, demonstrating the protocol. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
61336ee to
b1984f6
Compare
|
@maxrjones I have reduced the scope of this PR to make it easier to review. Now it's simply a protocol definition for one method ( |
This PR defines a
JSONSerializableprotocol inzarr.abc.serializablethat our metadata classes can use. That protocol is generic with 2 type parameters. 1 type parameter is used to declare the type that the class can be deserialized from, and the other type parameter declares the type the class serializes to. This allows our metadata classes to have generous input types, and narrow output types.The
JSONSerializableprotocol has 3 methods:to_json(serialization)from_json(deserialization from typed input)try_from_json(deserialization from arbitrary input; performs type checking before callingfrom_json)I implement these methods on the
ArrayV3Metadataclass, which involves the following changes:ArrayMetadataJSONLike_V3type, which is effectively the signature ofArrayV3Metadata.__init__data_typeparameter that theArrayV3Metadatawill accept. Now it accepts string or object declarations ofZDTypeinstances, instead of requiringZDTypeinstances.shapeparameter thatArrayV3Metadatawill accept. It now accepts aShapeLikeinput.ChunkGridLikeandCodecLikealiases for the inputsArrayV3Metadata.__init__acceptsDimensionNamesLikeas an alias forDimensionNamesThe goal is to make our array metadata class more useful by more clearly defining the types it accepts and creates. If we accept these changes, we can remove metadata parsing at higher levels in the codebase, and move towards making the
ArrayV3Metadataclass part of our public API.related:
#3786
#3795
edit: I have reduced the scope of this PR to make it easier to review. Now it's simply a protocol definition, and an implementation on
ArrayV3Metadata