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
- [ ] Fix model_validator type hints (before should take Any and narrow from there)
- [x] Better resource loading
- [x] Fix model_validator type hints (before should take Any and narrow from there)
- [ ] Sandbox for Jinja
- [ ] First-class addon support
- [ ] Language picker

View file

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

View file

@ -83,15 +83,19 @@ class Book(HexDocModel):
)
@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:
return data
context = cast_or_raise(info.context, I18nContext)
return data | {
"i18n_data": context.i18n,
"index_icon": data.get("index_icon") or data.get("model"),
}
match data:
case {**values}:
return values | {
"i18n_data": context.i18n,
"index_icon": values.get("index_icon") or values.get("model"),
}
case _:
return data
@model_validator(mode="after")
def _post_root(self, info: ValidationInfo) -> Self:

View file

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