Skip to content

Commit 06b5e8d

Browse files
committed
ENH: add result_type_false: check the minimum promotion rules according to the spec
Conforming array libraries may extend the minimum promotion rules. If they do, they will fail this new test and may want to xfail it.
1 parent ad81cf6 commit 06b5e8d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

array_api_tests/hypothesis_helpers.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from hypothesis import assume, reject
1111
from hypothesis.strategies import (SearchStrategy, booleans, composite, floats,
1212
integers, just, lists, none, one_of,
13-
sampled_from, shared, builds)
13+
sampled_from, shared, builds, permutations)
1414

1515
from . import _array_module as xp, api_version
1616
from . import array_helpers as ah
@@ -148,6 +148,25 @@ def mutually_promotable_dtypes(
148148
return one_of(strats).map(tuple)
149149

150150

151+
@composite
152+
def mutually_non_promotable_dtypes(
153+
draw,
154+
max_size: Optional[int] = 2,
155+
) -> Sequence[Tuple[DataType, ...]]:
156+
"""Generate a pair of dtypes which cannot be promoted."""
157+
assert max_size == 2
158+
159+
_categories = [
160+
(xp.bool,),
161+
dh.uint_dtypes + dh.int_dtypes,
162+
dh.real_float_dtypes + dh.complex_dtypes
163+
]
164+
cat_st = permutations(_categories).map(lambda s: s[:2])
165+
cat_from, cat_to = draw(cat_st)
166+
from_, to = draw(sampled_from(cat_from)), draw(sampled_from(cat_to))
167+
return from_, to
168+
169+
151170
class OnewayPromotableDtypes(NamedTuple):
152171
input_dtype: DataType
153172
result_dtype: DataType

array_api_tests/test_data_type_functions.py

+10
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,13 @@ def test_isdtype(dtype, kind):
204204
def test_result_type(dtypes):
205205
out = xp.result_type(*dtypes)
206206
ph.assert_dtype("result_type", in_dtype=dtypes, out_dtype=out, repr_name="out")
207+
208+
209+
@given(hh.mutually_non_promotable_dtypes(2))
210+
def test_result_type_false(dtypes):
211+
"""Test _very_ strict promotion rules according to the spec.
212+
Conforming array libraries may extend the promotion rules, and
213+
then they'll need to xfail this test.
214+
"""
215+
with pytest.raises(TypeError):
216+
xp.result_type(*dtypes)

0 commit comments

Comments
 (0)