Skip to content
Open
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
37 changes: 37 additions & 0 deletions tests/core/types/test_currency_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from web3.types import (
Gwei,
Wei,
)


def test_wei_arithmetic_preserves_type() -> None:
value = Wei(5)

value += Wei(1)

assert value == 6
assert isinstance(value, Wei)
assert isinstance(value + 1, Wei)
assert isinstance(1 + value, Wei)
assert isinstance(value - 1, Wei)
assert isinstance(10 - value, Wei)
assert isinstance(value * 2, Wei)
assert isinstance(2 * value, Wei)
assert isinstance(value // 2, Wei)
assert isinstance(value % 4, Wei)
assert isinstance(-value, Wei)
assert isinstance(+value, Wei)
assert isinstance(abs(value), Wei)
quotient, remainder = divmod(value, 4)
assert isinstance(quotient, Wei)
assert isinstance(remainder, Wei)


def test_gwei_arithmetic_preserves_type() -> None:
value = Gwei(5)

value -= Gwei(1)

assert value == 4
assert isinstance(value, Gwei)
assert isinstance(value + 1, Gwei)
48 changes: 46 additions & 2 deletions web3/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from web3._utils.compat import (
NotRequired,
Self,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -76,8 +77,51 @@
Nonce = NewType("Nonce", int)
RPCEndpoint = NewType("RPCEndpoint", str)
Timestamp = NewType("Timestamp", int)
Wei = NewType("Wei", int)
Gwei = NewType("Gwei", int)
class _IntegerType(int):
def __add__(self, other: int) -> Self:
return self.__class__(int(self) + other)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve non-integer arithmetic results or raise TypeError

The overridden arithmetic methods coerce every result back through self.__class__(...), which silently truncates non-integer numeric results instead of preserving Python’s normal semantics. For example, Wei(1) + 0.9 now becomes Wei(1) (via int(1.9)), and Wei(1) + Decimal('1.9') similarly drops the fractional part. This is a runtime correctness regression introduced by the new _IntegerType implementation and can corrupt values whenever callers mix Wei/Gwei with non-int numeric types.

Useful? React with 👍 / 👎.


def __radd__(self, other: int) -> Self:
return self.__class__(other + int(self))

def __sub__(self, other: int) -> Self:
return self.__class__(int(self) - other)

def __rsub__(self, other: int) -> Self:
return self.__class__(other - int(self))

def __mul__(self, other: int) -> Self:
return self.__class__(int(self) * other)

def __rmul__(self, other: int) -> Self:
return self.__class__(other * int(self))

def __floordiv__(self, other: int) -> Self:
return self.__class__(int(self) // other)

def __mod__(self, other: int) -> Self:
return self.__class__(int(self) % other)

def __divmod__(self, other: int) -> tuple[Self, Self]:
quotient, remainder = divmod(int(self), other)
return self.__class__(quotient), self.__class__(remainder)

def __neg__(self) -> Self:
return self.__class__(-int(self))

def __pos__(self) -> Self:
return self.__class__(+int(self))

def __abs__(self) -> Self:
return self.__class__(abs(int(self)))


class Wei(_IntegerType):
pass


class Gwei(_IntegerType):
pass
Formatters = dict[RPCEndpoint, Callable[..., Any]]


Expand Down