|
4 | 4 | from typing import Any, Callable, ClassVar, Dict, Generator, List, Sequence, Tuple, Type |
5 | 5 |
|
6 | 6 | import pytest |
7 | | -from pydantic import ConfigDict, Field, field_validator |
| 7 | +from pydantic import ConfigDict, Field, field_validator, model_serializer |
8 | 8 |
|
9 | 9 | from ethereum_clis import BlockExceptionWithMessage, Result, TransitionTool |
10 | 10 | from ethereum_test_base_types import ( |
@@ -141,15 +141,14 @@ class Header(CamelModel): |
141 | 141 | engine_api_error_code=EngineAPIError.InvalidParams, ) ``` |
142 | 142 | """ |
143 | 143 |
|
144 | | - model_config = ConfigDict( |
145 | | - arbitrary_types_allowed=True, |
146 | | - # explicitly set Removable items to None so they are not included in |
147 | | - # the serialization (in combination with exclude_None=True in |
148 | | - # model.dump()). |
149 | | - json_encoders={ |
150 | | - Removable: lambda x: None, |
151 | | - }, |
152 | | - ) |
| 144 | + model_config = ConfigDict(arbitrary_types_allowed=True) |
| 145 | + |
| 146 | + @model_serializer(mode="wrap", when_used="json") |
| 147 | + def _serialize_model(self, serializer, info): |
| 148 | + """Exclude Removable fields from serialization.""" |
| 149 | + del info |
| 150 | + data = serializer(self) |
| 151 | + return {k: v for k, v in data.items() if not isinstance(v, Removable)} |
153 | 152 |
|
154 | 153 | @field_validator("withdrawals_root", mode="before") |
155 | 154 | @classmethod |
|
0 commit comments