Skip to content

Commit

Permalink
Deprecate float sizes (min_size, max_size) (#1635)
Browse files Browse the repository at this point in the history
* add deprecation notice for float sizes

* test deprecation notice for float sizes

* add test for floats sizes deprecations

min_size and max_size should be integers. And we only test whole float numbers,
like 5.0 for example.

* change type hints of average_size to None

It use to be type: int

* add myself to the contributors list

* add relase.txt

* filter out deprecation warning during test

float size warning were breaking a test that was making sure that
invalid arguments raised an InvalidArgument error.

* fix formatting errors

lint and check-format tasks on travis

* fix pep8 linting error

* fix max/min size valitation and roll back tests

* updated release: add type hint infos

Thanks to @Zac-HD

* remove use of average_size in examples

* improve check_valid_size perf

* use % instead of .format in check_valid_size

Conforms to the rest of the document's style
  • Loading branch information
math2001 authored and Zac-HD committed Oct 11, 2018
1 parent 9064a8f commit 928e575
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
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
8 changes: 8 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,8 @@
RELEASE_TYPE: minor

This release deprecates using floats for ``min_size`` and ``max_size``.

The type hint for ``average_size`` arguments has been changed from
``Optional[int]`` to None, because non-None values are always ignored and
deprecated.

2 changes: 1 addition & 1 deletion hypothesis-python/docs/examples.rst
Expand Up @@ -100,7 +100,7 @@ First we need to define a strategy for Node:
NodeStrategy = s.builds(
Node,
s.integers(),
s.lists(s.booleans(), average_size=5, max_size=10))
s.lists(s.booleans(), max_size=10))
We want to generate *short* lists of values so that there's a decent chance of
one being a prefix of the other (this is also why the choice of bool as the
Expand Down
13 changes: 10 additions & 3 deletions hypothesis-python/src/hypothesis/internal/validation.py
Expand Up @@ -113,11 +113,18 @@ def check_valid_size(value, name):
'min_size=None is deprecated; use min_size=0 instead.'
)
return
check_type(integer_types + (float,), value)
if isinstance(value, float):
if math.isnan(value):
raise InvalidArgument(u'Invalid size %s=%r' % (name, value))
from hypothesis._settings import note_deprecation
note_deprecation(
'Float size are deprecated: '
'%s should be an integer, got %r' % (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):
raise InvalidArgument(u'Invalid size %s=%r' % (name, value))


@check_function
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
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

0 comments on commit 928e575

Please sign in to comment.