Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flag to allow only finite float values #228

Merged
merged 8 commits into from Aug 22, 2022

Conversation

PrettyWood
Copy link
Member

fix #224

src/input/input_json.rs Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2022

Codecov Report

Merging #228 (ed56450) into main (fdf42b8) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #228      +/-   ##
==========================================
+ Coverage   98.66%   98.69%   +0.02%     
==========================================
  Files          48       48              
  Lines        5031     5053      +22     
  Branches       35       35              
==========================================
+ Hits         4964     4987      +23     
+ Misses         67       66       -1     
Impacted Files Coverage Δ
src/errors/kinds.rs 99.15% <ø> (ø)
pydantic_core/_types.py 100.00% <100.00%> (ø)
src/input/input_json.rs 97.51% <100.00%> (ø)
src/input/input_python.rs 98.67% <100.00%> (ø)
src/validators/float.rs 98.90% <100.00%> (+0.13%) ⬆️
src/validators/typed_dict.rs 98.89% <0.00%> (+<0.01%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fdf42b8...ed56450. Read the comment docs.

@@ -115,23 +115,29 @@ impl<'a> Input<'a> for JsonInput {
}
}

fn strict_float(&self) -> ValResult<f64> {
fn strict_float(&self, _only_finite: bool) -> ValResult<f64> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't make sense for strict mode of JSON since a string is required to represent not finite values

),
],
)
def test_non_finite_json_values(py_and_json: PyAndJson, input_value, only_finite, expected):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is a bit verbose but more explicit and readable than with more parametrization. Do not hesitate to ask for a change or do it yourself @samuelcolvin.

@PrettyWood
Copy link
Member Author

@tiangolo are you happy with such a flag?

@PrettyWood PrettyWood changed the title add flag to allow only finite values for float validation add flag to allow only finite float values Aug 11, 2022
Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the idea looks good.

pydantic_core/_types.py Outdated Show resolved Hide resolved
src/errors/kinds.rs Outdated Show resolved Hide resolved
src/input/input_abstract.rs Outdated Show resolved Hide resolved
src/errors/kinds.rs Outdated Show resolved Hide resolved
tests/validators/test_float.py Outdated Show resolved Hide resolved
@PrettyWood
Copy link
Member Author

Thanks for the review! Feedback taken

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just waiting for @tiangolo's feedback.

@@ -59,6 +59,7 @@ class DictSchema(TypedDict, total=False):

class FloatSchema(TypedDict, total=False):
type: Required[Literal['float']]
allow_inf_nan: bool # whether 'NaN', '+inf', '-inf' should be forbidden. default: True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add allow_inf_nan to Config to otherwise it can't be set, might be good to add a test to confirm setting allow_inf_nan via config works.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes I added the implementation but not the interfaces. I'll do that soon(ish)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know Rust but this is great! 🚀

@samuelcolvin samuelcolvin merged commit 72b9cad into pydantic:main Aug 22, 2022
@samuelcolvin
Copy link
Member

this is great, thanks so much.

@PrettyWood PrettyWood deleted the finite-floats branch August 22, 2022 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

flag to prevent inf/nan to float fields
4 participants