Skip to content

Commit

Permalink
datamodel-codegen pydantic v2 output
Browse files Browse the repository at this point in the history
Mypy doesn't like these dict defaults in config.py, since they are not the same type as the annotated type. Can't we somehow avoid copying the same defaults twice here?
  • Loading branch information
visr committed Oct 17, 2023
1 parent ce4b332 commit 50cf27f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 79 deletions.
4 changes: 2 additions & 2 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ tests = { depends_on = ["lint", "test-ribasim-python", "test-ribasim-core"] }
# Codegen
generate-schema = "julia --project=docs docs/gen_schema.jl"
generate-python = """\
datamodel-codegen --use-title-as-name --use-double-quotes --disable-timestamp --use-default --strict-nullable --input-file-type=jsonschema --input docs/schema/root.schema.json --output python/ribasim/ribasim/models.py && \
datamodel-codegen --use-title-as-name --use-double-quotes --disable-timestamp --use-default --strict-nullable --input-file-type=jsonschema --input docs/schema/Config.schema.json --output python/ribasim/ribasim/config.py"""
datamodel-codegen --use-title-as-name --target-python-version=3.9 --use-double-quotes --disable-timestamp --use-default --strict-nullable --input-file-type=jsonschema --input docs/schema/root.schema.json --output-model-type=pydantic_v2.BaseModel --output python/ribasim/ribasim/models.py && \
datamodel-codegen --use-title-as-name --target-python-version=3.9 --use-double-quotes --disable-timestamp --use-default --strict-nullable --input-file-type=jsonschema --input docs/schema/Config.schema.json --output-model-type=pydantic_v2.BaseModel --output python/ribasim/ribasim/config.py"""
codegen = { depends_on = ["generate-schema", "generate-python", "lint"] }

[dependencies]
Expand Down
114 changes: 37 additions & 77 deletions python/ribasim/ribasim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime
from typing import List, Optional, Union

from pydantic import BaseModel, Field
from pydantic import BaseModel


class Output(BaseModel):
Expand Down Expand Up @@ -107,79 +107,39 @@ class Config(BaseModel):
input_dir: str = "."
output_dir: str = "."
geopackage: str
output: Output = Field(
default_factory=lambda: Output.parse_obj(
{
"basin": "output/basin.arrow",
"flow": "output/flow.arrow",
"control": "output/control.arrow",
"outstate": None,
"compression": "zstd",
"compression_level": 6,
}
)
)
solver: Solver = Field(
default_factory=lambda: Solver.parse_obj(
{
"algorithm": "QNDF",
"saveat": [],
"adaptive": True,
"dt": None,
"dtmin": None,
"dtmax": None,
"force_dtmin": False,
"abstol": 1e-06,
"reltol": 0.001,
"maxiters": 1000000000,
"sparse": True,
"autodiff": True,
}
)
)
logging: Logging = Field(
default_factory=lambda: Logging.parse_obj(
{"verbosity": {"level": 0}, "timing": False}
)
)
terminal: Terminal = Field(
default_factory=lambda: Terminal.parse_obj({"static": None})
)
pid_control: PidControl = Field(
default_factory=lambda: PidControl.parse_obj({"static": None, "time": None})
)
level_boundary: LevelBoundary = Field(
default_factory=lambda: LevelBoundary.parse_obj({"static": None, "time": None})
)
pump: Pump = Field(default_factory=lambda: Pump.parse_obj({"static": None}))
tabulated_rating_curve: TabulatedRatingCurve = Field(
default_factory=lambda: TabulatedRatingCurve.parse_obj(
{"static": None, "time": None}
)
)
user: User = Field(
default_factory=lambda: User.parse_obj({"static": None, "time": None})
)
flow_boundary: FlowBoundary = Field(
default_factory=lambda: FlowBoundary.parse_obj({"static": None, "time": None})
)
basin: Basin = Field(
default_factory=lambda: Basin.parse_obj(
{"profile": None, "state": None, "static": None, "time": None}
)
)
manning_resistance: ManningResistance = Field(
default_factory=lambda: ManningResistance.parse_obj({"static": None})
)
discrete_control: DiscreteControl = Field(
default_factory=lambda: DiscreteControl.parse_obj(
{"condition": None, "logic": None}
)
)
outlet: Outlet = Field(default_factory=lambda: Outlet.parse_obj({"static": None}))
linear_resistance: LinearResistance = Field(
default_factory=lambda: LinearResistance.parse_obj({"static": None})
)
fractional_flow: FractionalFlow = Field(
default_factory=lambda: FractionalFlow.parse_obj({"static": None})
)
output: Output = {
"basin": "output/basin.arrow",
"flow": "output/flow.arrow",
"control": "output/control.arrow",
"outstate": None,
"compression": "zstd",
"compression_level": 6,
}
solver: Solver = {
"algorithm": "QNDF",
"saveat": [],
"adaptive": True,
"dt": None,
"dtmin": None,
"dtmax": None,
"force_dtmin": False,
"abstol": 1e-06,
"reltol": 0.001,
"maxiters": 1000000000,
"sparse": True,
"autodiff": True,
}
logging: Logging = {"verbosity": {"level": 0}, "timing": False}
terminal: Terminal = {"static": None}
pid_control: PidControl = {"static": None, "time": None}
level_boundary: LevelBoundary = {"static": None, "time": None}
pump: Pump = {"static": None}
tabulated_rating_curve: TabulatedRatingCurve = {"static": None, "time": None}
user: User = {"static": None, "time": None}
flow_boundary: FlowBoundary = {"static": None, "time": None}
basin: Basin = {"profile": None, "state": None, "static": None, "time": None}
manning_resistance: ManningResistance = {"static": None}
discrete_control: DiscreteControl = {"condition": None, "logic": None}
outlet: Outlet = {"static": None}
linear_resistance: LinearResistance = {"static": None}
fractional_flow: FractionalFlow = {"static": None}

0 comments on commit 50cf27f

Please sign in to comment.