Skip to content

Commit

Permalink
add tests, tweak comments
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Nov 6, 2023
1 parent b653558 commit a9a8886
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/input/input_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'a> Input<'a> for PyAny {
str.as_bytes()
} else if let Ok(py_byte_array) = self.downcast::<PyByteArray>() {
// Safety: from_slice does not run arbitrary Python code and the GIL is held so the
// bytes array will not be mutated while from_slice is reading it
// bytes array will not be mutated while `JsonValue::parse` is reading it
unsafe { py_byte_array.as_bytes() }
} else {
return Err(ValError::new(ErrorTypeDefaults::JsonType, self));
Expand Down
1 change: 0 additions & 1 deletion src/input/input_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ impl<'a> Input<'a> for StringMapping<'a> {
match self {
Self::String(s) => {
let str = py_string_str(s)?;
// TODO control allow_inf_nan
JsonValue::parse(str.as_bytes(), true).map_err(|e| map_json_err(self, e))
}
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::JsonType, self)),
Expand Down
35 changes: 33 additions & 2 deletions tests/validators/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Any, Dict

import pytest
from dirty_equals import FunctionCheck, IsStr
from dirty_equals import FunctionCheck, IsFloatNan, IsStr

from pydantic_core import SchemaValidator, ValidationError
from pydantic_core import SchemaValidator, ValidationError, core_schema

from ..conftest import Err, PyAndJson, plain_repr

Expand Down Expand Up @@ -374,3 +374,34 @@ def test_string_with_underscores() -> None:
v.validate_python(edge_case)
with pytest.raises(ValidationError):
v.validate_json(f'"{edge_case}"')


def test_allow_inf_nan_true_json() -> None:
v = SchemaValidator(core_schema.float_schema())

assert v.validate_json('123') == 123
assert v.validate_json('NaN') == IsFloatNan()
assert v.validate_json('Infinity') == float('inf')
assert v.validate_json('-Infinity') == float('-inf')


def test_allow_inf_nan_false_json() -> None:
v = SchemaValidator(core_schema.float_schema(), core_schema.CoreConfig(allow_inf_nan=False))

assert v.validate_json('123') == 123
with pytest.raises(ValidationError) as exc_info1:
v.validate_json('NaN')
# insert_assert(exc_info.value.errors())
assert exc_info1.value.errors(include_url=False) == [
{'type': 'finite_number', 'loc': (), 'msg': 'Input should be a finite number', 'input': IsFloatNan()}
]
with pytest.raises(ValidationError) as exc_info2:
v.validate_json('Infinity')
assert exc_info2.value.errors(include_url=False) == [
{'type': 'finite_number', 'loc': (), 'msg': 'Input should be a finite number', 'input': float('inf')}
]
with pytest.raises(ValidationError) as exc_info3:
v.validate_json('-Infinity')
assert exc_info3.value.errors(include_url=False) == [
{'type': 'finite_number', 'loc': (), 'msg': 'Input should be a finite number', 'input': float('-inf')}
]
26 changes: 25 additions & 1 deletion tests/validators/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from dirty_equals import IsStr

from pydantic_core import SchemaValidator, ValidationError
from pydantic_core import SchemaValidator, ValidationError, core_schema

from ..conftest import Err, PyAndJson, plain_repr

Expand Down Expand Up @@ -472,3 +472,27 @@ class PlainEnum(Enum):
v_lax = v.validate_python(PlainEnum.ONE)
assert v_lax == 1
assert type(v_lax) == int


def test_allow_inf_nan_true_json() -> None:
v = SchemaValidator(core_schema.int_schema(), core_schema.CoreConfig(allow_inf_nan=True))

assert v.validate_json('123') == 123
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('NaN')
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('Infinity')
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('-Infinity')


def test_allow_inf_nan_false_json() -> None:
v = SchemaValidator(core_schema.int_schema(), core_schema.CoreConfig(allow_inf_nan=False))

assert v.validate_json('123') == 123
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('NaN')
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('Infinity')
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('-Infinity')

0 comments on commit a9a8886

Please sign in to comment.