Pydantic and singledispatch
I was reading about
pydantic-singledispatch [1]
from Giddeon’s blog and found it very intersting. I’m getting ready to
implement pydantic on my static site generator markata [2],
and I think there are so uses for this idea, so I want to try it out.
The Idea # [3]
Let’s set up some pydantic settings. We will need separate Models for each
environment that we want to support for this to work. The whole idea is to use
functools.singledispatch and type hints to provide unique execution for each
environment. We might want something like a path_prefix in prod for
environments like GithubPages that deploy to /<name-of-repo> while keeping
the root at / in dev.
Settings Model # [4]
Here is our model for our settings. We will create a CommonSettings model
that will be used by all environments. We will also create a DevSettings
model that will be used in dev and ProdSettings that will be used in prod.
We will use env as the discriminator so pydantic knows which model to use.
from typing im...