Skip to content

Commit b20ce16

Browse files
Add support boolean type
1 parent 22d6970 commit b20ce16

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

dpnp/dpnp_iface_mathematical.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,15 +2290,15 @@ def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
22902290
Whether to create a copy of `x` (``True``) or to replace values
22912291
in-place (``False``). The in-place operation only occurs if casting to
22922292
an array does not require a copy.
2293-
nan : {int, float}, optional
2293+
nan : {int, float, bool}, optional
22942294
Value to be used to fill ``NaN`` values.
22952295
Default: ``0.0``.
2296-
posinf : {int, float, None}, optional
2296+
posinf : {int, float, bool, None}, optional
22972297
Value to be used to fill positive infinity values. If no value is
22982298
passed then positive infinity values will be replaced with a very
22992299
large number.
23002300
Default: ``None``.
2301-
neginf : {int, float, None} optional
2301+
neginf : {int, float, bool, None} optional
23022302
Value to be used to fill negative infinity values. If no value is
23032303
passed then negative infinity values will be replaced with a very
23042304
small (or negative) number.
@@ -2347,9 +2347,12 @@ def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
23472347

23482348
dpnp.check_supported_arrays_type(x)
23492349

2350-
if isinstance(nan, bool) or not isinstance(nan, (int, float)):
2350+
# Python boolean is a subtype of an integer
2351+
# so additional check for bool is not needed.
2352+
if not isinstance(nan, (int, float)):
23512353
raise TypeError(
2352-
f"nan must be a scalar of an integer or float, but got {type(nan)}"
2354+
"nan must be a scalar of an integer, float, bool, "
2355+
f"but got {type(nan)}"
23532356
)
23542357

23552358
out = dpnp.empty_like(x) if copy else x
@@ -2368,17 +2371,17 @@ def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
23682371
)
23692372
max_f, min_f = _get_max_min(x.real.dtype)
23702373
if posinf is not None:
2371-
if isinstance(posinf, bool) or not isinstance(posinf, (int, float)):
2374+
if not isinstance(posinf, (int, float)):
23722375
raise TypeError(
2373-
"posinf must be a scalar of an integer or float, or None, "
2374-
f"but got {type(posinf)}"
2376+
"posinf must be a scalar of an integer, float, bool, "
2377+
f"or be None, but got {type(posinf)}"
23752378
)
23762379
max_f = posinf
23772380
if neginf is not None:
2378-
if isinstance(neginf, bool) or not isinstance(neginf, (int, float)):
2381+
if not isinstance(neginf, (int, float)):
23792382
raise TypeError(
2380-
"neginf must be a scalar of an integer or float, or None, "
2381-
f"but got {type(neginf)}"
2383+
"neginf must be a scalar of an integer, float, bool, "
2384+
f"or be None, but got {type(neginf)}"
23822385
)
23832386
min_f = neginf
23842387

tests/test_mathematical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ def test_errors(self):
11691169
assert_raises(TypeError, dpnp.nan_to_num, ia, neginf=i_neginf)
11701170

11711171
@pytest.mark.parametrize("kwarg", ["nan", "posinf", "neginf"])
1172-
@pytest.mark.parametrize("value", [True, 1 - 0j, [1, 2]])
1172+
@pytest.mark.parametrize("value", [1 - 0j, [1, 2], (1,)])
11731173
def test_errors_diff_types(self, kwarg, value):
11741174
ia = dpnp.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf])
11751175
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)