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
Changes from all commits
499d676
e49ae74
977f301
661cc31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enable the Hypothesis plugin to generate a constrained float when the `multiple_of` argument is specified. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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' | ||
max_value = math.floor(max_value / cls.multiple_of) | ||
if exclude_max: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be better to use the built-in |
||
max_value = max_value - 1 | ||
|
||
return st.integers(min_value, max_value).map(lambda x: x * cls.multiple_of) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This strategy may generate integers in the case of |
||
|
||
|
||
@resolves(pydantic.ConstrainedInt) | ||
|
There was a problem hiding this comment.
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
andmultiple_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 inhypothesis-jsonschema
(there are multiple places in the linked file) as it handles all these constraints.There was a problem hiding this comment.
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