Skip to content

Commit f3b4db4

Browse files
fix: align HexBytes32 hash with bytes (#258)
## Summary Align HexBytes32 hashing with bytes to restore the hash/equality contract for dict/set keys. ## Changes - Use `bytes.__hash__` in `HexBytes32.__hash__`. - Assert hash parity with `bytes(hb)` in `test_hexbytes32_repr_hex_strip_hash`. ## Testing - `./.venv/bin/python -m pytest` - Compiled extension loaded: `evmspec/_new.cpython-313-x86_64-linux-gnu.so` ## Risk and rollback - Low risk: only hash value changes. Roll back by reverting this commit.
1 parent 27deabe commit f3b4db4

File tree

2 files changed

+1
-5
lines changed

2 files changed

+1
-5
lines changed

evmspec/data/_main.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,6 @@ def __getitem__(self, key: SupportsIndex | slice) -> int | faster_hexbytes.HexBy
418418
# def __len__(self) -> Literal[32]:
419419
# return 32
420420

421-
def __hash__(self) -> int:
422-
# TODO: can we just remove this?
423-
return hash(_hex(self))
424-
425421
def hex(self) -> str: # type: ignore [override]
426422
"""
427423
Output hex-encoded bytes, with an "0x" prefix.

tests/data/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_hexbytes32_repr_hex_strip_hash() -> None:
7777
assert repr(hb) == f"HexBytes32(0x{bytes.hex(hb)})"
7878
assert hb.hex() == f"0x{bytes.hex(hb)}"
7979
assert hb.strip() == "12"
80-
assert hash(hb) == hash(bytes.hex(hb))
80+
assert hash(hb) == hash(bytes(hb))
8181
zeroed = HexBytes32("0x" + "00" * 32)
8282
assert zeroed.strip() == "0"
8383

0 commit comments

Comments
 (0)