Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,3 @@ jobs:
files: |
wasm/*.whl
prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') }}

- name: upload wasm to pypi
run: twine upload wasm/*
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.pypi_token }}
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pydantic-core"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
license = "MIT"
homepage = "https://github.com/pydantic/pydantic-core"
Expand Down
4 changes: 3 additions & 1 deletion src/validators/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ impl Validator for ConstrainedFloatValidator {
return Err(ValError::new(ErrorKind::FiniteNumber, input));
}
if let Some(multiple_of) = self.multiple_of {
if float % multiple_of != 0.0 {
let rem = float % multiple_of;
let threshold = float / 1e9;
if rem.abs() > threshold && (rem - multiple_of).abs() > threshold {
return Err(ValError::new(
ErrorKind::MultipleOf {
multiple_of: multiple_of.into(),
Expand Down
43 changes: 35 additions & 8 deletions tests/validators/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,53 @@ def test_float_strict(py_and_json: PyAndJson, input_value, expected):
({'le': 0}, 0.1, Err('Input should be less than or equal to 0')),
({'lt': 0}, 0, Err('Input should be less than 0')),
({'lt': 0.123456}, 1, Err('Input should be less than 0.123456')),
({'multiple_of': 0.5}, 0.5, 0.5),
({'multiple_of': 0.5}, 1, 1),
({'multiple_of': 0.5}, 0.6, Err('Input should be a multiple of 0.5')),
],
)
def test_float_kwargs(py_and_json: PyAndJson, kwargs: Dict[str, Any], input_value, expected):
v = py_and_json({'type': 'float', **kwargs})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_test(input_value)
errors = exc_info.value.errors()
assert len(errors) == 1
if 'context' in errors[0]:
assert errors[0]['context'] == kwargs
else:
output = v.validate_test(input_value)
assert output == expected
assert isinstance(output, float)


@pytest.mark.parametrize(
'multiple_of,input_value,error',
[
(0.5, 0.5, None),
(0.5, 1, None),
(0.5, 0.6, Err('Input should be a multiple of 0.5')),
(0.5, 0.51, Err('Input should be a multiple of 0.5')),
(0.5, 0.501, Err('Input should be a multiple of 0.5')),
(0.5, 1_000_000.5, None),
(0.5, 1_000_000.49, Err('Input should be a multiple of 0.5')),
(0.1, 0, None),
(0.1, 0.0, None),
(0.1, 0.2, None),
(0.1, 0.3, None),
(0.1, 0.4, None),
(0.1, 0.5, None),
(0.1, 0.5001, Err('Input should be a multiple of 0.1')),
(0.1, 1, None),
(0.1, 1.0, None),
(0.1, int(5e10), None),
],
ids=repr,
)
def test_float_multiple_of(py_and_json: PyAndJson, multiple_of, input_value, error):
v = py_and_json({'type': 'float', 'multiple_of': multiple_of})
if error:
with pytest.raises(ValidationError, match=re.escape(error.message)):
v.validate_test(input_value)
else:
output = v.validate_test(input_value)
assert output == input_value
assert isinstance(output, float)


def test_union_float(py_and_json: PyAndJson):
v = py_and_json(
{'type': 'union', 'choices': [{'type': 'float', 'strict': True}, {'type': 'float', 'multiple_of': 7}]}
Expand Down