Conversation
|
for context, we have a |
| ... | ||
|
|
||
| @classmethod | ||
| def try_from_json(cls, obj: object) -> Self: |
There was a problem hiding this comment.
| def try_from_json(cls, obj: object) -> Self: | |
| def from_object(cls, obj: object) -> Self: |
as discussed on zulip, I'm don't find try_from_json to be intuitive
There was a problem hiding this comment.
I don't think I'm a fan of from_object, because the term "object" is unfortunately overloaded in python. People might think "object" means "a class instance" as opposed to "the base type object". I'd like to get a range of opinions here. cc @zarr-developers/python-core-devs
There was a problem hiding this comment.
from_json_checked could convey that there's a type checking step...
There was a problem hiding this comment.
Could we require typed input for now and add the untyped method later if there's demand?
from_json_checked could convey that there's a type checking step...
I think it could also mean that it's from a checked json. what about from_untyped_json or from_untyped?
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