Fix before validator typing

This commit is contained in:
object-Object 2023-08-23 08:06:12 -04:00
parent 53175313b0
commit 96076c69ac
5 changed files with 44 additions and 30 deletions

View file

@ -1,5 +1,5 @@
- [ ] Better resource loading - [x] Better resource loading
- [ ] Fix model_validator type hints (before should take Any and narrow from there) - [x] Fix model_validator type hints (before should take Any and narrow from there)
- [ ] Sandbox for Jinja - [ ] Sandbox for Jinja
- [ ] First-class addon support - [ ] First-class addon support
- [ ] Language picker - [ ] Language picker

View file

@ -21,12 +21,12 @@ class PageWithPattern(PageWithText, type=None):
hex_size: int | None = None hex_size: int | None = None
@model_validator(mode="before") @model_validator(mode="before")
def _pre_root_patterns(cls, values: dict[str, Any]): def _pre_root_patterns(cls, values: Any):
# patterns may be a list or a single pattern, so make sure we always get a list match values:
patterns = values.get("patterns") case {"patterns": patterns} if not isinstance(patterns, (list, NoneType)):
if isinstance(patterns, (NoneType, list)): return values | {"patterns": [patterns]}
return values case _:
return values | {"patterns": [patterns]} return values
@property @property
def args(self) -> str | None: def args(self) -> str | None:
@ -46,12 +46,14 @@ class PageWithOpPattern(PageWithPattern, type=None):
op_id: ResourceLocation op_id: ResourceLocation
@model_validator(mode="before") @model_validator(mode="before")
def _pre_root_header(cls, values: dict[str, Any], info: ValidationInfo): def _pre_root_header(cls, values: Any, info: ValidationInfo):
if not info.context: if not info.context:
return values return values
context = cast_or_raise(info.context, I18nContext) context = cast_or_raise(info.context, I18nContext)
# use the pattern name as the header match values:
return values | { case {"op_id": op_id}:
"header": context.i18n.localize_pattern(values["op_id"]), # use the pattern name as the header
} return values | {"header": context.i18n.localize_pattern(op_id)}
case _:
return values

View file

@ -15,18 +15,22 @@ from .abstract_hex_pages import PageWithOpPattern, PageWithPattern
class LookupPatternPage(PageWithOpPattern, type="hexcasting:pattern"): class LookupPatternPage(PageWithOpPattern, type="hexcasting:pattern"):
@model_validator(mode="before") @model_validator(mode="before")
def _pre_root_lookup(cls, values: dict[str, Any], info: ValidationInfo): def _pre_root_lookup(cls, values: Any, info: ValidationInfo):
if not info.context: if not info.context:
return values return values
context = cast_or_raise(info.context, HexContext) context = cast_or_raise(info.context, HexContext)
# look up the pattern from the op id match values:
op_id = ResourceLocation.from_str(values["op_id"]) case {"op_id": op_id}:
pattern = context.patterns[op_id] # look up the pattern from the op id
return values | { id = ResourceLocation.from_str(op_id)
"op_id": op_id, pattern = context.patterns[id]
"patterns": [pattern], return values | {
} "op_id": id,
"patterns": [pattern],
}
case _:
return values
class ManualOpPatternPage( class ManualOpPatternPage(

View file

@ -83,15 +83,19 @@ class Book(HexDocModel):
) )
@model_validator(mode="before") @model_validator(mode="before")
def _pre_root(cls, data: dict[str, Any], info: ValidationInfo) -> dict[str, Any]: def _pre_root(cls, data: Any, info: ValidationInfo):
if not info.context: if not info.context:
return data return data
context = cast_or_raise(info.context, I18nContext) context = cast_or_raise(info.context, I18nContext)
return data | { match data:
"i18n_data": context.i18n, case {**values}:
"index_icon": data.get("index_icon") or data.get("model"), return values | {
} "i18n_data": context.i18n,
"index_icon": values.get("index_icon") or values.get("model"),
}
case _:
return data
@model_validator(mode="after") @model_validator(mode="after")
def _post_root(self, info: ValidationInfo) -> Self: def _post_root(self, info: ValidationInfo) -> Self:

View file

@ -72,10 +72,14 @@ class BaseResourceLocation:
return handler(values) return handler(values)
@field_validator("namespace", mode="before") @field_validator("namespace", mode="before")
def _default_namespace(cls, value: str | None) -> str: def _default_namespace(cls, value: Any):
if value is None: match value:
return "minecraft" case str():
return value.lower() return value.lower()
case None:
return "minecraft"
case _:
return value
@field_validator("path") @field_validator("path")
def _validate_path(cls, value: str): def _validate_path(cls, value: str):