Search code examples
pythonnumpypython-hypothesis

Why did Hypothesis give a falsifying example, when manually reproducing with numpy arrays does not fail?


I was trying to write my first ultra-simple numpy testcase, but the first thing I thought of seems to hit a roadblock.

So I did this:

import numpy as np
from hypothesis import given
import hypothesis.strategies as hs
import hypothesis.extra.numpy as hxn
    
def tstind(a, i):
     i = max(i, 0)
     i = min(i, len(a)-1)
     return a[i]
     
@given(a=hxn.arrays(dtype=hxn.scalar_dtypes(),
       shape=hxn.array_shapes(max_dims=1)),
       i=hs.integers())
def test_tstind_typeconserve(a, i):
     assert tstind(a, i).dtype == a.dtype
     
test_tstind_typeconserve()

Falsifying example:

test_tstind_typeconserve(
    a=array([0.], dtype=float16), i=0,
)

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test_tstind_typeconserve
  File "/tmp/persistent/miniconda3/envs/hypo/lib/python3.7/site-packages/hypothesis/core.py", line 1169, in wrapped_test
    raise the_error_hypothesis_found
  File "<stdin>", line 5, in test_tstind_typeconserve
AssertionError

But:

a=np.array([0.], dtype=np.float16)
i=0
assert tstind(a, i).dtype == a.dtype

(i.e. OK, does not fail)

BTW the odd case I was expecting it to find is something like this one :

a=np.ma.masked_array([0.], mask=[1], dtype=np.float16)
a.dtype
dtype('float16')
a[0].dtype
dtype('float64')

Solution

  • Hypothesis is showing you that Numpy datatypes have distinct byte orders. Expanding your test,

        got = tstind(a, i).dtype
        assert got == a.dtype, (a.dtype.byteorder, got.byteorder)
    

    fails for me with AssertionError: ('>', '='). It's unfortunate that the repr of array objects doesn't include the dtype byteorder, but here we are.

    (I've reported this as issue 19059, for what it's worth)