当特别要求不要时,假设策略会生成 inf

Hypothesis strategy generating inf when specifically asked not to

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)

如果你 运行 data_strategy.example() 几次,你会注意到结果中的一些值有无限实部或虚部。我在这里的目的是明确禁止无限或 NaN 部分。

我做错了什么?

更新:如果我使用

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

并将其转换为我测试中的数组,问题似乎消失了。复数溢出了吗?我没有收到关于假设溢出的常见弃用警告。

如果我使用

data_strategy = data_strategy_real

没有 inf 出现。

complex64类型太小,溢出了。假设以某种方式未能捕捉到这一点。

切换到 complex128 暂时解决了这个问题。

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

是的,这个问题的根本原因是您正在生成 64 位 有限浮点数,然后将它们转换为 32 位(因为 complex64 是一对 32 位浮点数)。您可以使用 floats():

width=32 参数来解决这个问题
floats_notnull_32 = partial(hs.floats, allow_nan=False, allow_infinity=False, width=32)

而且您没有得到通常的溢出检查,因为 it's only implemented for floats and integers at the moment. I've opened (edit: and fixed) issue #1591 也检查复杂类型和字符串类型。