-
Notifications
You must be signed in to change notification settings - Fork 18
Closed
Description
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:
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'}