mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
fix int_validator not catching overflows (#3112)
* fix int_validator not catching overflows * Update changes/3112-ojii.md Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Catch overflow errors in `int_validator`
|
||||
@@ -125,7 +125,7 @@ def int_validator(v: Any) -> int:
|
||||
|
||||
try:
|
||||
return int(v)
|
||||
except (TypeError, ValueError):
|
||||
except (TypeError, ValueError, OverflowError):
|
||||
raise errors.IntegerError()
|
||||
|
||||
|
||||
|
||||
@@ -43,6 +43,18 @@ def test_int_validation():
|
||||
assert Model(a=4.5).a == 4
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value', [2.2250738585072011e308, float('nan'), float('inf')])
|
||||
def test_int_overflow_validation(value):
|
||||
class Model(BaseModel):
|
||||
a: int
|
||||
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Model(a=value)
|
||||
assert exc_info.value.errors() == [
|
||||
{'loc': ('a',), 'msg': 'value is not a valid integer', 'type': 'type_error.integer'}
|
||||
]
|
||||
|
||||
|
||||
def test_frozenset_validation():
|
||||
class Model(BaseModel):
|
||||
a: frozenset
|
||||
|
||||
Reference in New Issue
Block a user