From 19377521bec392ab9c38fa98a8172d3f8f6974b7 Mon Sep 17 00:00:00 2001 From: antazoey Date: Mon, 10 Jun 2024 16:45:53 -0500 Subject: [PATCH] fix: block validation issues (#2125) --- src/ape/api/providers.py | 15 ++++++++++----- tests/functional/test_block.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/ape/api/providers.py b/src/ape/api/providers.py index a1b81c91bb..de2e5a9cf2 100644 --- a/src/ape/api/providers.py +++ b/src/ape/api/providers.py @@ -108,12 +108,17 @@ def validate_size(cls, values, handler): gets returned in computed field "size". """ - if not hasattr(values, "pop"): - # Handle weird AttributeDict missing pop method. - # https://github.com/ethereum/web3.py/issues/3326 - values = {**values} + if isinstance(values, BlockAPI): + size = values.size + + else: + if not hasattr(values, "pop"): + # Handle weird AttributeDict missing pop method. + # https://github.com/ethereum/web3.py/issues/3326 + values = {**values} + + size = values.pop("size", None) - size = values.pop("size", None) model = handler(values) if size is not None: model._size = size diff --git a/tests/functional/test_block.py b/tests/functional/test_block.py index dd77b9b96e..66db7f965f 100644 --- a/tests/functional/test_block.py +++ b/tests/functional/test_block.py @@ -1,5 +1,6 @@ import pytest from eth_pydantic_types import HexBytes +from web3.types import BlockData from ape_ethereum.ecosystem import Block @@ -73,3 +74,21 @@ def test_block_uncles(block): data["uncles"] = uncles actual = Block.model_validate(data) assert actual.uncles == uncles + + +def test_model_dump_and_validate(block): + model_dump = block.model_dump(by_alias=True) + model_validate = Block.model_validate(model_dump) + assert model_validate == block + # Validate existing model. + model_validate_from_model = Block.model_validate(block) + assert model_validate_from_model == block + + +def test_model_validate_web3_block(): + """ + Show we have good compatability with web3.py native types. + """ + data = BlockData(number=123, timestamp=123, gasLimit=123, gasUsed=100) # type: ignore + actual = Block.model_validate(data) + assert actual.number == 123