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

Allow for generation of a constrained float with multiple_of argument for hypothesis plugin #2442

Merged
merged 4 commits into from Mar 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2442-tobi-lipede-oodle.md
@@ -0,0 +1 @@
enable the Hypothesis plugin to generate a constrained float when the `multiple_of` argument is specified.
17 changes: 16 additions & 1 deletion pydantic/_hypothesis_plugin.py
Expand Up @@ -276,6 +276,7 @@ def resolve_confloat(cls): # type: ignore[no-untyped-def]
max_value = cls.le
exclude_min = False
exclude_max = False

if cls.gt is not None:
assert min_value is None, 'Set `gt` or `ge`, but not both'
min_value = cls.gt
Expand All @@ -284,7 +285,21 @@ def resolve_confloat(cls): # type: ignore[no-untyped-def]
assert max_value is None, 'Set `lt` or `le`, but not both'
max_value = cls.lt
exclude_max = True
return st.floats(min_value, max_value, exclude_min=exclude_min, exclude_max=exclude_max, allow_nan=False)

if cls.multiple_of is None:
return st.floats(min_value, max_value, exclude_min=exclude_min, exclude_max=exclude_max, allow_nan=False)

if min_value is not None:
min_value = math.ceil(min_value / cls.multiple_of)
if exclude_min:
min_value = min_value + 1
if max_value is not None:
assert max_value >= cls.multiple_of, 'Cannot build model with max value smaller than multiple of'

Choose a reason for hiding this comment

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

This assertion restricts the generation more than needed. For example, max_value=-2.0 and multiple_of=-1.0 will fail this assertion, but if the plugin generates -3.0, it satisfies the constraints.

Generally, I suggest taking a look at multipleOf implementation in hypothesis-jsonschema (there are multiple places in the linked file) as it handles all these constraints.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, will take a look

max_value = math.floor(max_value / cls.multiple_of)
if exclude_max:

Choose a reason for hiding this comment

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

It may be better to use the built-in exclude_max argument to st.floats instead of subtracting, as it reduces the possible range of generated values more than needed.

max_value = max_value - 1

return st.integers(min_value, max_value).map(lambda x: x * cls.multiple_of)

Choose a reason for hiding this comment

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

This strategy may generate integers in the case of multiple_of is an integer. I suggest using st.floats instead.



@resolves(pydantic.ConstrainedInt)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_hypothesis_plugin.py
Expand Up @@ -74,6 +74,8 @@ class ConstrainedNumbersModel(pydantic.BaseModel):
conintmul: pydantic.conint(ge=10, le=100, multiple_of=7)
confloatt: pydantic.confloat(gt=10, lt=100)
confloate: pydantic.confloat(ge=10, le=100)
confloatemul: pydantic.confloat(ge=10, le=100, multiple_of=4.2)
confloattmul: pydantic.confloat(gt=10, lt=100, multiple_of=10)
condecimalt: pydantic.condecimal(gt=10, lt=100)
condecimale: pydantic.condecimal(ge=10, le=100)

Expand Down