3
from functools import partial
import hypothesis as h
import hypothesis.strategies as hs
import hypothesis.extra.numpy as hnp
import numpy as np


floats_notnull = partial(hs.floats, allow_nan=False, allow_infinity=False)
complex_notnull = partial(hs.complex_numbers, allow_nan=False, allow_infinity=False)

data_strategy_real = hnp.arrays(
    np.float64,
    hs.tuples(hs.integers(min_value=2, max_value=50),
              hs.integers(min_value=2, max_value=5)),
    floats_notnull()
)

data_strategy_complex = hnp.arrays(
    np.complex64,
    hs.tuples(hs.integers(min_value=2, max_value=50), hs.just(1)),
    complex_notnull()
)

data_strategy = hs.one_of(data_strategy_real, data_strategy_complex)

If you run data_strategy.example() a couple times, you'll notice that some of the values in the result have infinite real or imaginary parts. My intention here was to specifically disallow infinite or NaN parts.

What am I doing wrong?

Update: if I use

data_strategy = hs.lists(complex_notnull, min_size=2, max_size=50)

and convert that to an array inside my test, the problem appears to go away. Are the complex numbers overflowing? I'm not getting the usual deprecation warning about overflow from Hypothesis.

And if I use

data_strategy = data_strategy_real

no infs appear.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96

2 Answers2

4

The complex64 type is too small and it's overflowing. Somehow Hypothesis is failing to catch this.

Yep, the root cause of this problem is that you're generating 64-bit finite floats, then casting them to 32-bit (because complex64 is a pair of 32-bit floats). You can fix that with the width=32 argument to floats():

floats_notnull_32 = partial(hs.floats, allow_nan=False, allow_infinity=False, width=32)

And you're not getting the usual overflow check because it's only implemented for floats and integers at the moment. I've opened (edit: and fixed) issue #1591 to check complex and string types too.

Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19
1

The complex64 type is too small and it's overflowing. Somehow Hypothesis is failing to catch this.

Switching to complex128 fixed the problem for now.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96