Skip to content

Consider using ClassVar for schema versioning #717

@bruno-f-cruz

Description

@bruno-f-cruz

Is your feature request related to a problem? Please describe.
There is currently no easy way to get a version of a schema without instantiating a model. This is a bit annoying as models are difficult to fully build to get validation. I suggest that we change the pattern to keep track of schema versions, eg:

https://github.com/AllenNeuralDynamics/aind-data-schema/blob/2c66f2e8a2f89a6b21caa86eb7377ab5aef88280/src/aind_data_schema/base.py#L25C1-L28C1

and instead, make the version of the schema also a class variable. This allows the value to be queried before the instance is created and will not change any of the serialization behavior.

Describe the solution you'd like
Here's a quick example:

from typing import Literal, ClassVar, get_args

from pydantic import BaseModel

_version = Literal["0.1.0"]


class Baz(BaseModel):
    """Example Foo task."""
    version_class: ClassVar[_version] = get_args(_version)[0]
    version: _version = get_args(_version)[0]


print(Baz.version_class)
#0.1.0
print(Baz.model_json_schema())
#{'description': 'Example Foo task.', 'properties': {'version': {'const': '0.1.0', 'default': '0.1.0', 'title': 'Version'}}, 'title': 'Baz', 'type': 'object'}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions