From 558092c8bb772dbe233dc8ac307b0f28e210f59d Mon Sep 17 00:00:00 2001 From: Miles Mason Winther <42948872+mmwinther@users.noreply.github.com> Date: Fri, 5 Jan 2024 14:15:20 +0100 Subject: [PATCH] Use custom Base Model (#36) * Use custom Base Model This allows us to set config to control Pydantic behaviour. * [GENERATE] Pydantic models from JSON Schema --------- Co-authored-by: dapla-bot[bot] <143391972+dapla-bot[bot]@users.noreply.github.com> --- .github/workflows/generate-python.yml | 1 + .../datadoc_model/datadoc_base_model.py | 7 ++++++ .../datadoc_model/datadoc_model/model.py | 22 ++++++++++--------- 3 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 generated/python/datadoc_model/datadoc_model/datadoc_base_model.py diff --git a/.github/workflows/generate-python.yml b/.github/workflows/generate-python.yml index 8728e7c..64760fe 100644 --- a/.github/workflows/generate-python.yml +++ b/.github/workflows/generate-python.yml @@ -53,6 +53,7 @@ jobs: --input-file-type jsonschema --input src/metadata-container-json-schema.json --output-model-type pydantic_v2.BaseModel + --base-class "datadoc_model.datadoc_base_model.DatadocBaseModel" --use-default --use-one-literal-as-default --force-optional diff --git a/generated/python/datadoc_model/datadoc_model/datadoc_base_model.py b/generated/python/datadoc_model/datadoc_model/datadoc_base_model.py new file mode 100644 index 0000000..fad7173 --- /dev/null +++ b/generated/python/datadoc_model/datadoc_model/datadoc_base_model.py @@ -0,0 +1,7 @@ +from pydantic import ConfigDict, BaseModel + + +class DatadocBaseModel(BaseModel): + """Defines configuration which applies to all Models in this package.""" + + model_config = ConfigDict(validate_assignment=True, use_enum_values=True) diff --git a/generated/python/datadoc_model/datadoc_model/model.py b/generated/python/datadoc_model/datadoc_model/model.py index 0a17f9a..7a86ce1 100644 --- a/generated/python/datadoc_model/datadoc_model/model.py +++ b/generated/python/datadoc_model/datadoc_model/model.py @@ -1,6 +1,6 @@ # generated by datamodel-codegen: # filename: metadata-container-json-schema.json -# timestamp: 2024-01-04T14:47:13+00:00 +# timestamp: 2024-01-05T12:58:00+00:00 from __future__ import annotations @@ -8,7 +8,9 @@ from typing import Any, Literal, Optional from uuid import UUID -from pydantic import AnyUrl, AwareDatetime, BaseModel, Field +from pydantic import AnyUrl, AwareDatetime, Field + +from datadoc_model.datadoc_base_model import DatadocBaseModel class Assessment(str, Enum): @@ -48,7 +50,7 @@ class VariableRole(str, Enum): ATTRIBUTE = "ATTRIBUTE" -class LanguageStringType(BaseModel): +class LanguageStringType(DatadocBaseModel): en: Optional[str] = None nn: Optional[str] = None nb: Optional[str] = None @@ -85,7 +87,7 @@ class TemporalityTypeType(str, Enum): EVENT = "EVENT" -class PseudoDataset(BaseModel): +class PseudoDataset(DatadocBaseModel): dataset_pseudo_time: Optional[AwareDatetime] = Field( None, description="Time at which the dataset was pseudonymized. In ISO 8601 format.", @@ -105,7 +107,7 @@ class SourceVariableDatatype(str, Enum): DATETIME = "DATETIME" -class PseudoVariable(BaseModel): +class PseudoVariable(DatadocBaseModel): short_name: Optional[str] = Field( None, description="Physical name of the variable in the dataset. Should match the recommended short name.", @@ -156,13 +158,13 @@ class PseudoVariable(BaseModel): ) -class PseudonymizationJsonSchema(BaseModel): +class PseudonymizationJsonSchema(DatadocBaseModel): document_version: Literal["0.1.0"] = "0.1.0" pseudo_dataset: Optional[PseudoDataset] = None pseudo_variables: Optional[list[PseudoVariable]] = None -class Dataset(BaseModel): +class Dataset(DatadocBaseModel): short_name: Optional[str] = Field( None, description="Name of (physical) data file, data table or dataset", @@ -274,7 +276,7 @@ class Dataset(BaseModel): ) -class Variable(BaseModel): +class Variable(DatadocBaseModel): short_name: Optional[str] = Field( None, description="Physical name of the variable in the dataset. Should match the recommended short name.", @@ -363,7 +365,7 @@ class Variable(BaseModel): ) -class DatadocJsonSchema(BaseModel): +class DatadocJsonSchema(DatadocBaseModel): percentage_complete: Optional[int] = Field( None, description="Percentage of obligatory metadata fields populated." ) @@ -374,7 +376,7 @@ class DatadocJsonSchema(BaseModel): variables: Optional[list[Variable]] = None -class MetadataContainer(BaseModel): +class MetadataContainer(DatadocBaseModel): document_version: Literal["0.0.1"] = "0.0.1" datadoc: Optional[DatadocJsonSchema] = None pseudonymization: Optional[PseudonymizationJsonSchema] = None