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

Improved the check_type and check_valid_size validation functions #2084

Merged
merged 3 commits into from Sep 9, 2019
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 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`.
12 changes: 8 additions & 4 deletions hypothesis-python/src/hypothesis/extra/numpy.py
Expand Up @@ -26,7 +26,7 @@
from hypothesis import Verbosity
from hypothesis._settings import note_deprecation
from hypothesis.errors import InvalidArgument
from hypothesis.internal.compat import hrange, integer_types
from hypothesis.internal.compat import PY2, hrange, integer_types
from hypothesis.internal.coverage import check_function
from hypothesis.internal.reflection import proxies
from hypothesis.internal.validation import check_type, check_valid_interval
Expand Down Expand Up @@ -615,11 +615,15 @@ def array_dtypes(
"""Return a strategy for generating array (compound) dtypes, with members
drawn from the given subtype strategy."""
order_check("size", 0, min_size, max_size)
native_strings = st.from_type(str).filter(bool) # See issue #1798 re: filter!
elements = st.tuples(native_strings, subtype_strategy)
# Field names must be native strings and the empty string is weird; see #1963.
if PY2:
field_names = st.binary(min_size=1)
else:
field_names = st.text(min_size=1)
elements = st.tuples(field_names, subtype_strategy)
if allow_subarrays:
elements |= st.tuples(
native_strings, subtype_strategy, array_shapes(max_dims=2, max_side=2)
field_names, subtype_strategy, array_shapes(max_dims=2, max_side=2)
)
return st.lists(
elements=elements,
Expand Down
3 changes: 2 additions & 1 deletion hypothesis-python/src/hypothesis/internal/coverage.py
Expand Up @@ -50,7 +50,8 @@ def pretty_file_name(f):
pass

parts = f.split(os.path.sep)
parts = parts[-parts[::-1].index("hypothesis") :]
if "hypothesis" in parts:
parts = parts[-parts[::-1].index("hypothesis") :]
result = os.path.sep.join(parts)
pretty_file_name_cache[f] = result
return result
Expand Down
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
13 changes: 13 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,15 @@ 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.
def type_checker(x):
check_type((int, str), x, "x")

type_checker(1)
type_checker("1")
with pytest.raises(InvalidArgument, match="Expected one of int, str but got "):
type_checker(1.0)