问题描述
我试图编写我的第一个超简单的 numpy 测试用例,但我想到的第一件事似乎遇到了障碍。
所以我这样做了:
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()
伪造示例:
test_tstind_typeconserve(
a=array([0.],dtype=float16),i=0,)
错误:
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
但是:
a=np.array([0.],dtype=np.float16)
i=0
assert tstind(a,i).dtype == a.dtype
(即OK,不会失败)
顺便说一句,我期待它找到的奇怪情况是这样的:
a=np.ma.masked_array([0.],mask=[1],dtype=np.float16)
a.dtype
dtype('float16')
a[0].dtype
dtype('float64')
解决方法
假设向您展示了 Numpy 数据类型具有 distinct byte orders。扩展您的测试,
got = tstind(a,i).dtype
assert got == a.dtype,(a.dtype.byteorder,got.byteorder)
AssertionError: ('>','=')
对我来说失败了。不幸的是,数组对象的 repr
不包含 dtype 字节顺序,但我们在这里。
(我已将此报告为 issue 19059,这是值得的)