|
1 | 1 | import warnings |
2 | 2 |
|
| 3 | +import numpy as np |
3 | 4 | import pytest |
4 | | - |
5 | 5 | from numpy.testing import assert_raises |
6 | | -import numpy as np |
7 | 6 |
|
8 | 7 | from .._creation_functions import asarray |
9 | | -from .._data_type_functions import astype, can_cast, isdtype, result_type |
10 | | -from .._dtypes import ( |
11 | | - bool, int8, int16, uint8, float64, int64 |
12 | | -) |
| 8 | +from .._data_type_functions import astype, can_cast, finfo, iinfo, isdtype, result_type |
| 9 | +from .._dtypes import bool, float64, int8, int16, int64, uint8 |
13 | 10 | from .._flags import set_array_api_strict_flags |
14 | 11 |
|
15 | 12 |
|
@@ -88,3 +85,40 @@ def test_result_type_py_scalars(api_version): |
88 | 85 |
|
89 | 86 | with pytest.raises(TypeError): |
90 | 87 | result_type(int64, True) |
| 88 | + |
| 89 | + |
| 90 | +def test_finfo_iinfo_dtypelike(): |
| 91 | + """np.finfo() and np.iinfo() accept any DTypeLike. |
| 92 | + Array API only accepts Array | DType. |
| 93 | + """ |
| 94 | + match = "must be a dtype or array" |
| 95 | + with pytest.raises(TypeError, match=match): |
| 96 | + finfo("float64") |
| 97 | + with pytest.raises(TypeError, match=match): |
| 98 | + finfo(float) |
| 99 | + with pytest.raises(TypeError, match=match): |
| 100 | + iinfo("int8") |
| 101 | + with pytest.raises(TypeError, match=match): |
| 102 | + iinfo(int) |
| 103 | + |
| 104 | + |
| 105 | +def test_finfo_iinfo_wrap_output(): |
| 106 | + """Test that the finfo(...).dtype and iinfo(...).dtype |
| 107 | + are array-api-strict.DType objects; not numpy.dtype. |
| 108 | + """ |
| 109 | + # Note: array_api_strict.DType objects are not singletons |
| 110 | + assert finfo(float64).dtype == float64 |
| 111 | + assert iinfo(int8).dtype == int8 |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize("func,arg", [(finfo, float64), (iinfo, int8)]) |
| 115 | +def test_finfo_iinfo_output_assumptions(func, arg): |
| 116 | + """There should be no expectation for the output of finfo()/iinfo() |
| 117 | + to be comparable, hashable, or writeable. |
| 118 | + """ |
| 119 | + obj = func(arg) |
| 120 | + assert obj != func(arg) # Defaut behaviour for custom classes |
| 121 | + with pytest.raises(TypeError): |
| 122 | + hash(obj) |
| 123 | + with pytest.raises(Exception, match="cannot assign"): |
| 124 | + obj.min = 0 |
0 commit comments