Skip to content

Commit

Permalink
Config: inherit all pydantic models from a common base class (#11857)
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd authored Jan 7, 2025
1 parent 267a2c1 commit 61d6bd3
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions readthedocs/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,32 @@
but we aren't using it yet, and instead we are doing the validation
in a separate step.
"""

from typing import Literal

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


class ConfigBaseModel(BaseModel):
"""
Base class for all the models used in the configuration object.
Useful to define common configuration options for all the models.
"""

model_config = ConfigDict(
# Don't allow extra fields in the models.
# It will raise an error if there are extra fields.
extra="forbid",
)


class BuildTool(BaseModel):
class BuildTool(ConfigBaseModel):
version: str
full_version: str


class BuildJobsBuildTypes(BaseModel):
class BuildJobsBuildTypes(ConfigBaseModel):
"""Object used for `build.jobs.build` key."""

html: list[str] | None = None
Expand All @@ -27,7 +42,7 @@ class BuildJobsBuildTypes(BaseModel):
htmlzip: list[str] | None = None


class BuildJobs(BaseModel):
class BuildJobs(ConfigBaseModel):
"""Object used for `build.jobs` key."""

pre_checkout: list[str] = []
Expand All @@ -46,52 +61,52 @@ class BuildJobs(BaseModel):


# TODO: rename this class to `Build`
class BuildWithOs(BaseModel):
class BuildWithOs(ConfigBaseModel):
os: str
tools: dict[str, BuildTool]
jobs: BuildJobs = BuildJobs()
apt_packages: list[str] = []
commands: list[str] = []


class PythonInstallRequirements(BaseModel):
class PythonInstallRequirements(ConfigBaseModel):
requirements: str


class PythonInstall(BaseModel):
class PythonInstall(ConfigBaseModel):
path: str
method: Literal["pip", "setuptools"] = "pip"
extra_requirements: list[str] = []


class Python(BaseModel):
class Python(ConfigBaseModel):
install: list[PythonInstall | PythonInstallRequirements] = []


class Conda(BaseModel):
class Conda(ConfigBaseModel):
environment: str


class Sphinx(BaseModel):
class Sphinx(ConfigBaseModel):
configuration: str | None
# NOTE: This is how we save the object in the DB,
# the actual options for users are "html", "htmldir", "singlehtml".
builder: Literal["sphinx", "sphinx_htmldir", "sphinx_singlehtml"] = "sphinx"
fail_on_warning: bool = False


class Mkdocs(BaseModel):
class Mkdocs(ConfigBaseModel):
configuration: str | None
fail_on_warning: bool = False


class Submodules(BaseModel):
class Submodules(ConfigBaseModel):
include: list[str] | Literal["all"] = []
exclude: list[str] | Literal["all"] = []
recursive: bool = False


class Search(BaseModel):
class Search(ConfigBaseModel):
ranking: dict[str, int] = {}
ignore: list[str] = [
"search.html",
Expand Down

0 comments on commit 61d6bd3

Please sign in to comment.