Skip to content

Commit f5653f3

Browse files
committed
Inherit from eth-utils CamelModel for pydantic types:
- The ``CustomPydanticModel`` used here inspired a more general class to be used across our libraries. That change was introduced in the latest ``eth-utils`` which we now use as a a bottom-pinned dependency.
1 parent b8fc03a commit f5653f3

3 files changed

Lines changed: 11 additions & 81 deletions

File tree

eth_account/_utils/transaction_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Any,
33
)
44

5+
from eth_utils import (
6+
CamelModel,
7+
)
58
from toolz import (
69
assoc,
710
dissoc,
@@ -13,9 +16,6 @@
1316
is_rpc_structured_access_list,
1417
is_rpc_structured_authorization_list,
1518
)
16-
from eth_account.datastructures import (
17-
CustomPydanticModel,
18-
)
1919
from eth_account.types import (
2020
AccessList,
2121
AuthorizationList,
@@ -82,7 +82,7 @@ def json_serialize_classes_in_transaction(val: Any) -> Any:
8282
- ``exclude=val._exclude: Fields excluded for serialization are defined within a
8383
``_exclude`` property on the pydantic model.
8484
"""
85-
if isinstance(val, CustomPydanticModel):
85+
if isinstance(val, CamelModel):
8686
return val.model_dump(by_alias=True)
8787
elif isinstance(val, dict):
8888
return {k: json_serialize_classes_in_transaction(v) for k, v in val.items()}

eth_account/datastructures.py

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
SupportsIndex,
55
overload,
66
)
7-
import warnings
87

98
from eth_keys.datatypes import (
109
Signature,
@@ -13,30 +12,16 @@
1312
ChecksumAddress,
1413
)
1514
from eth_utils import (
15+
CamelModel,
1616
to_checksum_address,
1717
)
1818
from hexbytes import (
1919
HexBytes,
2020
)
2121
from pydantic import (
22-
BaseModel,
23-
ConfigDict,
2422
Field,
2523
field_serializer,
2624
)
27-
from pydantic.alias_generators import (
28-
to_camel,
29-
)
30-
from pydantic.json_schema import (
31-
DEFAULT_REF_TEMPLATE,
32-
GenerateJsonSchema,
33-
JsonSchemaMode,
34-
JsonSchemaValue,
35-
)
36-
from pydantic_core import (
37-
CoreSchema,
38-
PydanticOmit,
39-
)
4025

4126

4227
class SignedTransaction(
@@ -105,64 +90,7 @@ def __getitem__(self, index: SupportsIndex | slice | str) -> Any:
10590
raise TypeError("Index must be an integer, slice, or string")
10691

10792

108-
class OmitJsonSchema(GenerateJsonSchema):
109-
def handle_invalid_for_json_schema(
110-
self, schema: CoreSchema, error_info: str
111-
) -> JsonSchemaValue:
112-
raise PydanticOmit
113-
114-
115-
class CustomPydanticModel(BaseModel):
116-
"""
117-
A base class for eth-account pydantic models that provides custom JSON-RPC
118-
serialization configuration. JSON-RPC serialization is configured to use
119-
camelCase keys and to exclude fields that are marked as ``exclude=True``. To
120-
serialize a model to the expected JSON-RPC format, use
121-
``model_dump(by_alias=True)``.
122-
"""
123-
124-
model_config = ConfigDict(
125-
arbitrary_types_allowed=True,
126-
populate_by_name=True, # populate by snake_case (python) args
127-
alias_generator=to_camel, # serialize by camelCase (json-rpc) keys
128-
)
129-
130-
def recursive_model_dump(self) -> Any:
131-
# TODO: Remove in next major release. This was introduced because of a bug with
132-
# a ``@field_serializer`` decorator not being applied correctly to nested
133-
# models. This is no longer necessary.
134-
warnings.warn(
135-
"recursive_model_dump() is deprecated. Please use "
136-
"model_dump(by_alias=True) instead.",
137-
DeprecationWarning,
138-
stacklevel=2,
139-
)
140-
return self.model_dump(by_alias=True)
141-
142-
@classmethod
143-
def model_json_schema(
144-
cls,
145-
by_alias: bool = True,
146-
ref_template: str = DEFAULT_REF_TEMPLATE,
147-
# default to ``OmitJsonSchema`` to prevent errors from excluded fields
148-
schema_generator: type[GenerateJsonSchema] = OmitJsonSchema,
149-
mode: JsonSchemaMode = "validation",
150-
**kwargs: Any,
151-
) -> dict[str, Any]:
152-
"""
153-
Omits excluded fields from the JSON schema, preventing errors that would
154-
otherwise be raised by the default schema generator.
155-
"""
156-
return super().model_json_schema(
157-
by_alias=by_alias,
158-
ref_template=ref_template,
159-
schema_generator=schema_generator,
160-
mode=mode,
161-
**kwargs,
162-
)
163-
164-
165-
class SignedSetCodeAuthorization(CustomPydanticModel):
93+
class SignedSetCodeAuthorization(CamelModel):
16694
chain_id: int
16795
address: bytes
16896
nonce: int

tests/core/_test_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from eth_keys.datatypes import (
22
Signature,
33
)
4+
from eth_utils import (
5+
CamelModel,
6+
)
47
from hexbytes import (
58
HexBytes,
69
)
@@ -9,7 +12,6 @@
912
)
1013

1114
from eth_account.datastructures import (
12-
CustomPydanticModel,
1315
SignedSetCodeAuthorization,
1416
)
1517

@@ -41,15 +43,15 @@
4143
}
4244

4345

44-
class PydanticTestClassInner(CustomPydanticModel):
46+
class PydanticTestClassInner(CamelModel):
4547
int_value: int = 2
4648
str_value: str = "3"
4749
authorization_list: list[SignedSetCodeAuthorization] = [TEST_SIGNED_AUTHORIZATION]
4850
excluded_field1: str = Field(default="4", exclude=True)
4951
excluded_field2: int = Field(default=5, exclude=True)
5052

5153

52-
class PydanticTestClass(CustomPydanticModel):
54+
class PydanticTestClass(CamelModel):
5355
int_value: int = 1
5456
nested_model: PydanticTestClassInner = PydanticTestClassInner()
5557
excluded_field1: str = Field(default="6", exclude=True)

0 commit comments

Comments
 (0)