Skip to content

Commit

Permalink
Improved the check_type and check_valid_size validation functions
Browse files Browse the repository at this point in the history
A couple of minor improvements have been made:
- The check_type function now tests for lists of length 1 so the
returned mesaging is better.
- check_valid_size() now passes the name variable into check_type
as this is a required argument.

To cover the additional logic within check_type a new test has
been added in the test_validation file.
  • Loading branch information
tg committed Sep 6, 2019
1 parent 6ac1eef commit fccad72
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTING.rst
Expand Up @@ -269,6 +269,7 @@ their individual contributions.
* `Sushobhit <https://github.com/sushobhit27>`_ (sushobhitsolanki@gmail.com)
* `Tariq Khokhar <https://www.github.com/tkb>`_ (tariq@khokhar.net)
* `Tessa Bradbury <https://www.github.com/tessereth>`_
* `Thomas Grainge <https://www.github.com/tgrainge>`_
* `Tim Martin <https://www.github.com/timmartin>`_ (tim@asymptotic.co.uk)
* `Tom McDermott <https://www.github.com/sponster-au>`_ (sponster@gmail.com)
* `Tyler Gibbons <https://www.github.com/kavec>`_ (tyler.gibbons@flexport.com)
Expand Down
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch improves the messaging that comes from invalid size arguments
to collection strategies such as :func:`~hypothesis.strategies.lists`.
4 changes: 3 additions & 1 deletion hypothesis-python/src/hypothesis/internal/validation.py
Expand Up @@ -31,6 +31,8 @@ def check_type(typ, arg, name=""):
if name:
name += "="
if not isinstance(arg, typ):
if isinstance(typ, tuple) and len(typ) == 1:
typ = typ[0]
if isinstance(typ, tuple):
typ_string = "one of %s" % (", ".join(t.__name__ for t in typ))
else:
Expand Down Expand Up @@ -120,7 +122,7 @@ def check_valid_size(value, name):
since="2018-10-11",
)
else:
check_type(integer_types, value)
check_type(integer_types, value, name)
if value < 0:
raise InvalidArgument(u"Invalid size %s=%r < 0" % (name, value))

Expand Down
8 changes: 8 additions & 0 deletions hypothesis-python/tests/cover/test_validation.py
Expand Up @@ -23,6 +23,7 @@

from hypothesis import find, given
from hypothesis.errors import InvalidArgument
from hypothesis.internal.validation import check_type
from hypothesis.strategies import (
binary,
booleans,
Expand Down Expand Up @@ -247,3 +248,10 @@ def test(x):

with pytest.raises(InvalidArgument):
test()


def test_check_type_with_tuple_of_length_two():
# This test covers logic for length-two tuples that is essential on PY2,
# e.g. string_types (str, unicode) which are all length-one on Python 3.
check_type((int, str), 1)
check_type((int, str), "1")

0 comments on commit fccad72

Please sign in to comment.