Improve model naming

This commit is contained in:
object-Object 2023-08-25 00:32:10 -04:00
parent 7b910897f2
commit c9f4941bad
8 changed files with 38 additions and 32 deletions

View file

@ -5,6 +5,7 @@
- [ ] GitHub Actions
- [ ] Pypi releasing
- [ ] API improvements (disable exporting, make it easier (possible?) to generate book for imported mods)
- [ ] Unit test for mock addon book
- [ ] Re-add edified wood recipe to [Common/src/main/resources/assets/hexcasting/patchouli_books/thehexbook/en_us/entries/items/edified.json](items/edified) when it actually exists
- [ ] Language picker
- [ ] Version picker
- [ ] Re-add edified wood recipe to [Common/src/main/resources/assets/hexcasting/patchouli_books/thehexbook/en_us/entries/items/edified.json](items/edified) when it actually exists

View file

@ -15,7 +15,7 @@ requires-python = ">=3.11"
dependencies = [
"typing_extensions>=4.7.0",
"importlib_resources>=6.0.1",
"pydantic>=2.2.0",
"pydantic>=2.3.0",
"Jinja2>=3.1.2",
"pyjson5>=1.6.3",
]

View file

@ -22,7 +22,7 @@ from hexdoc.utils.deserialize import (
decode_and_flatten_json_dict,
isinstance_or_raise,
)
from hexdoc.utils.model import HexDocValidationContext
from hexdoc.utils.model import ValidationContext
@total_ordering
@ -201,5 +201,5 @@ class I18n:
return self.localize(f"key.{key}")
class I18nContext(HexDocValidationContext):
class I18nContext(ValidationContext):
i18n: I18n

View file

@ -2,7 +2,7 @@ __all__ = [
"HexDocModel",
"InternallyTaggedUnion",
"Color",
"HexDocValidationContext",
"ValidationContext",
"DEFAULT_CONFIG",
"NoValue",
"NoValueType",
@ -18,7 +18,7 @@ __all__ = [
"LoaderContext",
]
from .model import DEFAULT_CONFIG, HexDocModel, HexDocValidationContext
from .model import DEFAULT_CONFIG, HexDocModel, ValidationContext
from .properties import Properties, PropsContext
from .resource import Entity, ItemStack, ResLoc, ResourceLocation
from .resource_loader import LoaderContext, ModResourceLoader

View file

@ -1,21 +1,30 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Any, dataclass_transform
from typing import TYPE_CHECKING, Any, Self, dataclass_transform
from pydantic import BaseModel, ConfigDict, model_validator
from pydantic.config import ConfigDict
if TYPE_CHECKING:
from pydantic.root_model import Model
DEFAULT_CONFIG = ConfigDict(
extra="forbid",
)
@dataclass_transform()
class ValidationContext(BaseModel):
"""Base class for Pydantic validation context for `HexDocModel`."""
model_config = DEFAULT_CONFIG
@dataclass_transform()
class HexDocModel(BaseModel):
"""Base class for most Pydantic models in hexdoc.
Includes type overrides to require using subclasses of `ValidationContext` for
validation context.
"""
model_config = DEFAULT_CONFIG
# pydantic core actually allows PyAny for context, so I'm pretty sure this is fine
@ -23,32 +32,28 @@ class HexDocModel(BaseModel):
@classmethod
def model_validate( # pyright: ignore[reportIncompatibleMethodOverride]
cls: type[Model],
cls,
obj: Any,
*,
strict: bool | None = None,
from_attributes: bool | None = None,
context: HexDocValidationContext | None = None,
) -> Model:
context: ValidationContext | None = None,
) -> Self:
...
@classmethod
def model_validate_json( # pyright: ignore[reportIncompatibleMethodOverride]
cls: type[Model],
cls,
json_data: str | bytes | bytearray,
*,
strict: bool | None = None,
context: HexDocValidationContext | None = None,
) -> Model:
context: ValidationContext | None = None,
) -> Self:
...
class HexDocValidationContext(HexDocModel):
pass
@dataclass_transform()
class HexDocStripHiddenModel(HexDocModel):
class StripHiddenModel(HexDocModel):
"""Base model which removes all keys starting with _ before validation."""
@model_validator(mode="before")

View file

@ -6,7 +6,7 @@ from typing import Annotated, Any, Self
from pydantic import AfterValidator, HttpUrl
from .model import HexDocModel, HexDocStripHiddenModel, HexDocValidationContext
from .model import HexDocModel, StripHiddenModel, ValidationContext
from .resource import ResourceDir, ResourceLocation
from .toml_placeholders import load_toml_with_placeholders
@ -21,12 +21,12 @@ class HexDocMeta(HexDocModel):
book_url: NoTrailingSlashHttpUrl
class PatternStubProps(HexDocStripHiddenModel):
class PatternStubProps(StripHiddenModel):
path: Path
regex: re.Pattern[str]
class XplatProps(HexDocStripHiddenModel):
class XplatProps(StripHiddenModel):
src: Path
pattern_stubs: list[PatternStubProps] | None = None
resources: Path
@ -37,11 +37,11 @@ class PlatformProps(XplatProps):
tags: Path
class I18nProps(HexDocStripHiddenModel):
class I18nProps(StripHiddenModel):
default_lang: str
class Properties(HexDocStripHiddenModel):
class Properties(StripHiddenModel):
modid: str
book: ResourceLocation
url: NoTrailingSlashHttpUrl
@ -81,5 +81,5 @@ class Properties(HexDocStripHiddenModel):
return f"{base_url}/{id.file_path_stub('assets').as_posix()}"
class PropsContext(HexDocValidationContext):
class PropsContext(ValidationContext):
props: Properties

View file

@ -36,7 +36,7 @@ from pydantic.dataclasses import dataclass
from pydantic.functional_validators import ModelWrapValidatorHandler
from .deserialize import JSONDict
from .model import DEFAULT_CONFIG, HexDocModel, HexDocValidationContext
from .model import DEFAULT_CONFIG, HexDocModel, ValidationContext
HEXDOC_EXPORTS_GROUP = "hexdoc.export"
"""Entry point group name for bundled hexdoc data."""
@ -331,7 +331,7 @@ class HexDocIDModel(HexDocModel, ABC):
resource_dir: PathResourceDir,
id: ResourceLocation,
data: JSONDict,
context: HexDocValidationContext,
context: ValidationContext,
) -> Self:
logging.getLogger(__name__).debug(f"Load {cls} at {id}")
return cls.model_validate(

View file

@ -10,7 +10,7 @@ from typing import Callable, Literal, Self, TypeVar, overload
from pydantic.dataclasses import dataclass
from hexdoc.utils.deserialize import JSONDict, decode_json_dict
from hexdoc.utils.model import DEFAULT_CONFIG, HexDocModel, HexDocValidationContext
from hexdoc.utils.model import DEFAULT_CONFIG, HexDocModel, ValidationContext
from hexdoc.utils.types import without_suffix
from .properties import Properties
@ -343,5 +343,5 @@ class ModResourceLoader:
out_path.write_text(out_data, "utf-8")
class LoaderContext(HexDocValidationContext):
class LoaderContext(ValidationContext):
loader: ModResourceLoader