Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def format_code(code: str) -> str:
str(formatter_path),
"format",
str(input_file_path),
"--quiet",
"--no-cache",
"--config",
str(config_path),
Expand All @@ -100,7 +99,8 @@ def format_code(code: str) -> str:
if result.returncode != 0:
raise Exception(
f"Error formatting code using formatter '{formatter_path}': "
f"{result.stderr}"
f"returncode={result.returncode}, stdout={result.stdout!r}, "
Comment thread
danceratopz marked this conversation as resolved.
f"stderr={result.stderr!r}"
)

# Return the formatted source code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Transaction-related types for Ethereum tests."""

from dataclasses import dataclass
import decimal
from enum import IntEnum
from functools import cached_property
import numbers
from typing import Any, ClassVar, Dict, Generic, List, Literal, Self, Sequence

import ethereum_rlp as eth_rlp
Expand Down Expand Up @@ -842,6 +844,48 @@ def signer_minimum_balance(self, *, fork: Fork) -> int:
else:
return gas_price * gas_limit + self.value

def _format_field_value(self, value: Any) -> str:
"""
Format a field value for string representation.

Uses hex encoding for any value that supports it
(Address, Bytes, Hash, HexNumber, etc).
Comment thread
felix314159 marked this conversation as resolved.
Outdated
"""
if value is None:
return "None"

# fields like 'value' should be shown as decimal number
if isinstance(value, numbers.Number):
if isinstance(value, decimal.Decimal):
# Convert to string to avoid scientific notation (1E-9)
# while removing unnecessary trailing zeros (100.000000 -> 100)
return "{:f}".format(value).rstrip("0").rstrip(".")

Comment thread
felix314159 marked this conversation as resolved.
Outdated
return str(value)
Comment thread
felix314159 marked this conversation as resolved.

# fields like 'to' should be shown as hex string
if hasattr(value, "hex") and callable(value.hex):
return f'"{value.hex()}"'

return repr(value)

def __repr__(self) -> str:
"""
Return string representation with hex-encoded values for
applicable fields.
"""
field_strs = []
for field_name in self.__class__.model_fields:
value = getattr(self, field_name)
formatted_value = self._format_field_value(value)
field_strs.append(f"{field_name}={formatted_value}")

return f"{self.__class__.__name__}({', '.join(field_strs)})"

def __str__(self) -> str:
"""Return the repr string representation."""
return self.__repr__()


class NetworkWrappedTransaction(CamelModel, RLPSerializable):
"""
Expand Down
Loading