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

Deprecate float sizes (min_size, max_size) #1635

Merged
merged 14 commits into from Oct 11, 2018
1 change: 1 addition & 0 deletions CONTRIBUTING.rst
Expand Up @@ -230,6 +230,7 @@ their individual contributions.
* `Marius Gedminas <https://www.github.com/mgedmin>`_ (marius@gedmin.as)
* `Markus Unterwaditzer <http://github.com/untitaker/>`_ (markus@unterwaditzer.net)
* `Matt Bachmann <https://www.github.com/bachmann1234>`_ (bachmann.matt@gmail.com)
* `Mathieu Paturel <https://github.com/math2001>`_ (mathieu.paturel@gmail.com)
* `Max Nordlund <https://www.github.com/maxnordlund>`_ (max.nordlund@gmail.com)
* `Maxim Kulkin <https://www.github.com/maximkulkin>`_ (maxim.kulkin@gmail.com)
* `mulkieran <https://www.github.com/mulkieran>`_
Expand Down
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,3 @@
RELEASE_TYPE: minor

This release deprecates using floats for ``min_size`` and ``max_size``.
10 changes: 8 additions & 2 deletions hypothesis-python/src/hypothesis/internal/validation.py
Expand Up @@ -106,14 +106,20 @@ def check_valid_size(value, name):

Otherwise raises InvalidArgument.
"""
from hypothesis._settings import note_deprecation
math2001 marked this conversation as resolved.
Show resolved Hide resolved
if value is None:
if name == 'min_size':
from hypothesis._settings import note_deprecation
note_deprecation(
'min_size=None is deprecated; use min_size=0 instead.'
)
return
check_type(integer_types + (float,), value)
if isinstance(value, float):
note_deprecation(
'Float size are deprecated: '
'{} should be an integer, got {}'.format(name, value)
)
else:
check_type(integer_types, value)
if value < 0:
raise InvalidArgument(u'Invalid size %s=%r < 0' % (name, value))
if isinstance(value, float) and math.isnan(value):
Copy link
Member

@Zac-HD Zac-HD Oct 11, 2018

Choose a reason for hiding this comment

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

We can move this to the isinstance(value, float) clause above.

Moving it before the deprecation will also fix the failing test!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum... I don't know how I didn't see this... 😢 I'll make sure to read the whole function next time 👀

Expand Down
10 changes: 5 additions & 5 deletions hypothesis-python/src/hypothesis/strategies.py
Expand Up @@ -602,7 +602,7 @@ def sampled_from(elements):
def lists(
elements=None, # type: SearchStrategy[Ex]
min_size=0, # type: int
average_size=None, # type: int
average_size=None, # type: None
max_size=None, # type: int
unique_by=None, # type: Callable[..., Any]
unique=False, # type: bool
Expand Down Expand Up @@ -672,7 +672,7 @@ def unique_by(x):
def sets(
elements=None, # type: SearchStrategy[Ex]
min_size=0, # type: int
average_size=None, # type: int
average_size=None, # type: None
max_size=None, # type: int
):
# type: (...) -> SearchStrategy[Set[Ex]]
Expand Down Expand Up @@ -702,7 +702,7 @@ def sets(
def frozensets(
elements=None, # type: SearchStrategy[Ex]
min_size=0, # type: int
average_size=None, # type: int
average_size=None, # type: None
max_size=None, # type: int
):
# type: (...) -> SearchStrategy[FrozenSet[Ex]]
Expand Down Expand Up @@ -789,7 +789,7 @@ def dictionaries(
values, # type: SearchStrategy[T]
dict_class=dict, # type: type
min_size=0, # type: int
average_size=None, # type: int
average_size=None, # type: None
max_size=None, # type: int
):
# type: (...) -> SearchStrategy[Dict[Ex, T]]
Expand Down Expand Up @@ -937,7 +937,7 @@ def characters(
def text(
alphabet=None, # type: Union[Sequence[Text], SearchStrategy[Text]]
min_size=0, # type: int
average_size=None, # type: int
average_size=None, # type: None
max_size=None # type: int
):
# type: (...) -> SearchStrategy[Text]
Expand Down
6 changes: 5 additions & 1 deletion hypothesis-python/tests/cover/test_direct_strategies.py
Expand Up @@ -154,8 +154,12 @@ def fn_ktest(*fnkwargs):
'blacklist_categories': ['Nd']}),
)
def test_validates_keyword_arguments(fn, kwargs):
from hypothesis.errors import HypothesisDeprecationWarning
with pytest.raises(InvalidArgument):
fn(**kwargs).example()
# pytest.warns filters out the warning. It doesn't ensure that it
math2001 marked this conversation as resolved.
Show resolved Hide resolved
# raises a warning.
with pytest.warns(HypothesisDeprecationWarning):
fn(**kwargs).example()


@fn_ktest(
Expand Down
12 changes: 12 additions & 0 deletions hypothesis-python/tests/cover/test_validation.py
Expand Up @@ -31,6 +31,18 @@ def test_min_size_none_behavior():
lists(integers(), min_size=None).example()


@checks_deprecated_behaviour
@given(lists(integers(), min_size=5.0))
def test_min_size_float_behaviour(arr):
assert len(arr) >= 5


@checks_deprecated_behaviour
@given(lists(integers(), max_size=5.0))
def test_max_size_float_behaviour(arr):
assert len(arr) <= 5


def test_errors_when_given_varargs():
@given(integers())
def has_varargs(*args):
Expand Down