Skip to content

Commit b42d78b

Browse files
committed
Clip raises ValueError when types cannot be resolved
1 parent 7930810 commit b42d78b

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

dpctl/tensor/_clip.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def _clip_none(x, val, out, order, _binary_fn):
246246
_fp16 = sycl_dev.has_aspect_fp16
247247
_fp64 = sycl_dev.has_aspect_fp64
248248
if not _can_cast(val_dtype, res_dt, _fp16, _fp64):
249-
raise TypeError(
249+
raise ValueError(
250250
f"function 'clip' does not support input types "
251251
f"({x_dtype}, {val_dtype}), "
252252
"and the inputs could not be safely coerced to any "
@@ -267,7 +267,7 @@ def _clip_none(x, val, out, order, _binary_fn):
267267
)
268268

269269
if res_dt != out.dtype:
270-
raise TypeError(
270+
raise ValueError(
271271
f"Output array of type {res_dt} is needed, got {out.dtype}"
272272
)
273273

@@ -527,7 +527,7 @@ def clip(x, min=None, max=None, out=None, order="K"):
527527
)
528528

529529
if res_dt is None:
530-
raise TypeError(
530+
raise ValueError(
531531
f"function '{clip}' does not support input types "
532532
f"({x_dtype}, {min_dtype}, {max_dtype}), "
533533
"and the inputs could not be safely coerced to any "
@@ -550,7 +550,7 @@ def clip(x, min=None, max=None, out=None, order="K"):
550550
)
551551

552552
if res_dt != out.dtype:
553-
raise TypeError(
553+
raise ValueError(
554554
f"Output array of type {res_dt} is needed, "
555555
f"got {out.dtype}"
556556
)

dpctl/tests/test_tensor_clip.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ def test_clip_dtypes(dt1, dt2):
8282
assert dpt.all(r == ar1)
8383
assert r.sycl_queue == ar1.sycl_queue
8484
else:
85-
with pytest.raises(TypeError):
85+
with pytest.raises(ValueError):
8686
dpt.clip(ar1, ar2, ar3)
87-
with pytest.raises(TypeError):
87+
with pytest.raises(ValueError):
8888
dpt.clip(ar1, min=ar3, max=None)
89-
with pytest.raises(TypeError):
89+
with pytest.raises(ValueError):
9090
dpt.clip(ar1, min=None, max=ar3)
9191

9292

@@ -217,19 +217,15 @@ def test_clip_out_need_temporary_none():
217217
assert dpt.all(a_min[:-6] == 2) and dpt.all(a_min[-6:] == 3)
218218

219219

220-
def test_where_arg_validation():
220+
def test_clip_arg_validation():
221221
get_queue_or_skip()
222222

223223
check = dict()
224224
x1 = dpt.empty((1,), dtype="i4")
225225
x2 = dpt.empty((1,), dtype="i4")
226226

227227
with pytest.raises(TypeError):
228-
dpt.where(check, x1, x2)
229-
with pytest.raises(TypeError):
230-
dpt.where(x1, check, x2)
231-
with pytest.raises(TypeError):
232-
dpt.where(x1, x2, check)
228+
dpt.clip(check, x1, x2)
233229

234230

235231
@pytest.mark.parametrize(
@@ -381,7 +377,7 @@ def test_clip_dtype_error():
381377
ar4 = dpt.empty_like(ar1, dtype="f4")
382378

383379
assert_raises_regex(
384-
TypeError,
380+
ValueError,
385381
"Output array of type.*is needed",
386382
dpt.clip,
387383
ar1,
@@ -390,7 +386,7 @@ def test_clip_dtype_error():
390386
ar4,
391387
)
392388
assert_raises_regex(
393-
TypeError,
389+
ValueError,
394390
"Output array of type.*is needed",
395391
dpt.clip,
396392
ar1,
@@ -550,7 +546,7 @@ def test_clip_out_type_check():
550546
out = range(10)
551547

552548
with pytest.raises(TypeError):
553-
dpt.add(x1, x2, x3, out=out)
549+
dpt.clip(x1, x2, x3, out=out)
554550

555551

556552
@pytest.mark.parametrize("dt", ["i4", "f4", "c8"])
@@ -614,7 +610,7 @@ def test_clip_minmax_weak_types():
614610
y = dpt.clip(x, min_v, max_v)
615611
assert isinstance(y, dpt.usm_ndarray)
616612
else:
617-
with pytest.raises(TypeError):
613+
with pytest.raises(ValueError):
618614
dpt.clip(x, min_v, max_v)
619615

620616

@@ -624,8 +620,8 @@ def test_clip_max_weak_types():
624620
x = dpt.zeros(10, dtype="i4")
625621
m = dpt.ones(10, dtype="i4")
626622

627-
with pytest.raises(TypeError):
623+
with pytest.raises(ValueError):
628624
dpt.clip(x, m, 2.5)
629625

630-
with pytest.raises(TypeError):
626+
with pytest.raises(ValueError):
631627
dpt.clip(x, 2.5, m)

0 commit comments

Comments
 (0)