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:
Jonas Obrist
2022-08-05 20:22:55 +09:00
committed by GitHub
parent c73bf444db
commit 88ade4bb6d
3 changed files with 14 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
Catch overflow errors in `int_validator`
+1 -1
View File
@@ -125,7 +125,7 @@ def int_validator(v: Any) -> int:
try:
return int(v)
except (TypeError, ValueError):
except (TypeError, ValueError, OverflowError):
raise errors.IntegerError()
+12
View File
@@ -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