diff --git a/changes/3112-ojii.md b/changes/3112-ojii.md new file mode 100644 index 0000000..a4c6e14 --- /dev/null +++ b/changes/3112-ojii.md @@ -0,0 +1 @@ +Catch overflow errors in `int_validator` \ No newline at end of file diff --git a/pydantic/validators.py b/pydantic/validators.py index 2c65658..897d1bb 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -125,7 +125,7 @@ def int_validator(v: Any) -> int: try: return int(v) - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): raise errors.IntegerError() diff --git a/tests/test_validators.py b/tests/test_validators.py index f7d7016..e7007ad 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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