Skip to content

Commit

Permalink
fix: delay hex-int validation error when None values for non-optional (
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Oct 26, 2024
1 parent 54dcc57 commit 09492dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ape/types/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@


def _hex_int_validator(value, info):
if value is None:
# If not optional, will allow pydantic to (better) handle the error.
return value

# NOTE: Allows this module to load lazier.
access = import_module("ape.utils.basemodel").ManagerAccessMixin

convert = access.conversion_manager.convert
return convert(value, int)

Expand Down
15 changes: 14 additions & 1 deletion tests/functional/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from eth_utils import to_hex
from ethpm_types.abi import EventABI
from hexbytes import HexBytes
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, ValidationError

from ape.types.address import AddressType
from ape.types.basic import HexInt
Expand Down Expand Up @@ -144,6 +144,19 @@ class MyModel(BaseModel):
assert act.ual == expected
assert act.ual_optional is None

def test_none(self):
"""
Was getting unhelpful conversion errors here. We should instead
let Pydantic fail as it normally does in this situation.
"""

class MyModel(BaseModel):
an_int: HexInt

expected = ".*Input should be a valid integer.*"
with pytest.raises(ValidationError, match=expected):
_ = MyModel(an_int=None)


class TestCurrencyValueComparable:
def test_use_for_int_in_pydantic_model(self):
Expand Down

0 comments on commit 09492dc

Please sign in to comment.