Skip to content

fix: preserve Wei and Gwei types in arithmetic#3849

Open
samsamtrum wants to merge 1 commit into
ApeWorX:mainfrom
samsamtrum:fix-wei-gwei-arithmetic-types
Open

fix: preserve Wei and Gwei types in arithmetic#3849
samsamtrum wants to merge 1 commit into
ApeWorX:mainfrom
samsamtrum:fix-wei-gwei-arithmetic-types

Conversation

@samsamtrum
Copy link
Copy Markdown

What was wrong?

Wei and Gwei were NewType aliases over int. Static type checkers treat arithmetic on a NewType as returning the base int, so common accumulation patterns lose their unit type and augmented assignment is rejected.

For example:

value = Wei(5)
value += Wei(1)  # mypy sees the expression as int

How was it fixed?

This changes Wei and Gwei to lightweight int subclasses backed by a shared internal _IntegerType helper. The helper preserves the concrete subclass for common integer arithmetic operations such as addition, subtraction, multiplication, floor division, modulo, divmod, and unary operations.

Because the classes still subclass int, existing runtime behavior remains compatible with code that expects integers.

Verification

  • python3 -m py_compile web3/types.py tests/core/types/test_currency_types.py
  • mypy web3/types.py tests/core/types/test_currency_types.py
  • pytest tests/core/types/test_currency_types.py -q

Closes #3833

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6055bad9c2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread web3/types.py
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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wei and Gwei lose their type through arithmetic — += / -= rejected by mypy

1 participant