Schema Generation#
To allow for python typing of documents, we define them as TypedDict
in event_model.documents
.
from typing import Any, Dict
from typing_extensions import Annotated, TypedDict
from .generate.type_wrapper import Field, add_extra_schema
DATUM_EXTRA_SCHEMA = {"additionalProperties": False}
@add_extra_schema(DATUM_EXTRA_SCHEMA)
class Datum(TypedDict):
"""Document to reference a quanta of externally-stored data"""
datum_id: Annotated[
str,
Field(
description="Globally unique identifier for this Datum (akin to 'uid' "
"for other Document types), typically formatted as '<resource>/<integer>'"
),
]
datum_kwargs: Annotated[
Dict[str, Any],
Field(
description="Arguments to pass to the Handler to "
"retrieve one quanta of data",
),
]
resource: Annotated[
str, Field(description="The UID of the Resource to which this Datum belongs")
]
We then use pydantic to convert these python types into the jsonschema in event_model.schemas
.
After changing any of the documents it’s necessary to regenerate the schemas. This can be done by running:
regenerate-schema
which is a python environment script in a dev install of event-model.
This ensures we can have accurate typing across the bluesky codebase, but also doesn’t limit us to python for validating documents.