frozen pydantic models

This commit is contained in:
John Lancaster
2026-06-21 12:26:12 -05:00
parent 4320a251f5
commit 9a9432cc55
10 changed files with 309 additions and 119 deletions
+20 -2
View File
@@ -1,5 +1,8 @@
import re
from collections.abc import Mapping
from pathlib import PurePosixPath
from types import MappingProxyType
from typing import TypeVar
from pydantic import BaseModel
from pydantic import ConfigDict
@@ -8,10 +11,25 @@ from pydantic import field_validator
SKILL_ID_RE = re.compile(r"^[a-z][a-z0-9-]*$")
SEMVER_RE = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:[-+][0-9A-Za-z.-]+)?$")
K = TypeVar("K")
V = TypeVar("V")
class ReferenceEntry(BaseModel):
model_config = ConfigDict(extra="ignore", str_strip_whitespace=True)
class StrictFrozenModel(BaseModel):
model_config = ConfigDict(
extra="forbid",
frozen=True,
validate_by_alias=True,
validate_by_name=True,
str_strip_whitespace=True,
)
def frozen_mapping(value: Mapping[K, V] | None = None) -> Mapping[K, V]:
return MappingProxyType(dict(value or {}))
class ReferenceEntry(StrictFrozenModel):
path: str
mime_type: str = "text/markdown"
title: str | None = None