Skip to content

Commit 98abd4a

Browse files
authored
blocks json field didn't use camelCase (#157)
* blocks json field didn't use camelCase * review fixes * doc comment * ruff format
1 parent 22f2e44 commit 98abd4a

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

packages/testing/src/consensus_testing/test_fixtures/state_transition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def serialize_blocks(self, value: List[BlockSpec]) -> List[dict[str, Any]]:
9494
The serialized Blocks.
9595
"""
9696
del value
97-
return [block.model_dump(mode="json") for block in self._filled_blocks]
97+
return [block.to_json() for block in self._filled_blocks]
9898

9999
@field_serializer("expect_exception", when_used="json")
100100
def serialize_exception(self, value: type[Exception] | None) -> str | None:

packages/testing/src/consensus_testing/test_types/step_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def serialize_block(self, value: BlockSpec) -> dict[str, Any]:
108108
"Block not filled yet - make_fixture() must be called before serialization. "
109109
"This BlockStep should only be serialized after the fixture has been processed."
110110
)
111-
return self._filled_block.model_dump(mode="json")
111+
return self._filled_block.to_json()
112112

113113

114114
class AttestationStep(BaseForkChoiceStep):

packages/testing/src/framework/test_fixtures/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ def json_dict(self) -> Dict[str, Any]:
6262
6363
Excludes the `info` field and converts snake_case to camelCase.
6464
"""
65-
return self.model_dump(
66-
mode="json",
67-
by_alias=True,
65+
return self.to_json(
6866
exclude_none=True,
6967
exclude={"info"},
7068
)

src/lean_spec/types/base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Reusable, strict base models for the specification."""
22

3-
from typing import Any, Self
3+
from typing import Any, Dict, Self
44

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

30+
def to_json(self, **kwargs: Any) -> Dict[str, Any]:
31+
"""Return json encodable representation of this model"""
32+
# remove these if user tries to pass them
33+
kwargs.pop("mode", None)
34+
kwargs.pop("by_alias", None)
35+
36+
return self.model_dump(
37+
mode="json",
38+
by_alias=True,
39+
**kwargs,
40+
)
41+
3042

3143
class StrictBaseModel(CamelModel):
3244
"""A strict, immutable pydantic base model."""

0 commit comments

Comments
 (0)