Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def serialize_blocks(self, value: List[BlockSpec]) -> List[dict[str, Any]]:
The serialized Blocks.
"""
del value
return [block.model_dump(mode="json") for block in self._filled_blocks]
return [block.to_json() for block in self._filled_blocks]

@field_serializer("expect_exception", when_used="json")
def serialize_exception(self, value: type[Exception] | None) -> str | None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def serialize_block(self, value: BlockSpec) -> dict[str, Any]:
"Block not filled yet - make_fixture() must be called before serialization. "
"This BlockStep should only be serialized after the fixture has been processed."
)
return self._filled_block.model_dump(mode="json")
return self._filled_block.to_json()


class AttestationStep(BaseForkChoiceStep):
Expand Down
4 changes: 1 addition & 3 deletions packages/testing/src/framework/test_fixtures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def json_dict(self) -> Dict[str, Any]:

Excludes the `info` field and converts snake_case to camelCase.
"""
return self.model_dump(
mode="json",
by_alias=True,
return self.to_json(
exclude_none=True,
exclude={"info"},
)
Expand Down
14 changes: 13 additions & 1 deletion src/lean_spec/types/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Reusable, strict base models for the specification."""

from typing import Any, Self
from typing import Any, Dict, Self

from pydantic import BaseModel, ConfigDict
from pydantic.alias_generators import to_camel
Expand All @@ -27,6 +27,18 @@ def copy(self: Self, **kwargs: Any) -> Self:
"""Create a copy of the model with the updated fields that are validated."""
return self.__class__(**(self.model_dump(exclude_unset=True) | kwargs))

def to_json(self, **kwargs: Any) -> Dict[str, Any]:
"""Return json encodable representation of this model"""
# remove these if user tries to pass them
kwargs.pop("mode", None)
kwargs.pop("by_alias", None)

return self.model_dump(
mode="json",
by_alias=True,
**kwargs,
)


class StrictBaseModel(CamelModel):
"""A strict, immutable pydantic base model."""
Expand Down
Loading