Skip to content

Commit 28e435e

Browse files
committed
Apply review comments
1 parent 1f57cf8 commit 28e435e

3 files changed

Lines changed: 43 additions & 20 deletions

File tree

numpy/_core/numerictypes.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,14 @@ def isdtype(input_dtype, kind):
396396
False
397397
398398
"""
399-
input_dtype = dtype(input_dtype).type
399+
# validate and preprocess arguments
400+
if not isinstance(input_dtype, (type, dtype)):
401+
raise TypeError(
402+
"input_dtype argument must be a dtype, "
403+
f"but it is a {type(input_dtype)}."
404+
)
405+
if isinstance(input_dtype, dtype):
406+
input_dtype = input_dtype.type
400407
input_kinds = kind if isinstance(kind, tuple) else (kind,)
401408

402409
processed_kinds = set()
@@ -420,9 +427,17 @@ def isdtype(input_dtype, kind):
420427
sctypes["float"] + sctypes["complex"]
421428
)
422429
else:
423-
processed_kinds.add(dtype(kind).type)
424-
425-
return any([input_dtype == k for k in processed_kinds])
430+
# validate and preprocess kind
431+
if not isinstance(kind, (type, dtype)):
432+
raise TypeError(
433+
"kind argument must be comprised of dtypes or "
434+
f"strings only, but it has a {type(kind)}."
435+
)
436+
if isinstance(kind, dtype):
437+
kind = kind.type
438+
processed_kinds.add(kind)
439+
440+
return input_dtype in processed_kinds
426441

427442

428443
@set_module('numpy')

numpy/_core/numerictypes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def issubclass_(arg1: object, arg2: object) -> L[False]: ...
110110
def issubsctype(arg1: DTypeLike, arg2: DTypeLike) -> bool: ...
111111

112112
def isdtype(
113-
input_dtype: DTypeLike,
113+
input_dtype: dtype[Any] | type[Any],
114114
kind: DTypeLike | tuple[DTypeLike, ...]
115115
) -> bool: ...
116116

numpy/_core/tests/test_numerictypes.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from numpy._core.numerictypes import (
77
issctype, sctype2char, maximum_sctype, sctypes
88
)
9-
from numpy.testing import assert_, assert_equal, assert_raises, IS_PYPY
9+
from numpy.testing import (
10+
assert_, assert_equal, assert_raises, assert_raises_regex, IS_PYPY
11+
)
1012

1113
# This is the structure of the table used for plain objects:
1214
#
@@ -416,20 +418,6 @@ def test_nondtype_nonscalartype(self):
416418
assert np.issubdtype(np.float32, "f")
417419

418420

419-
@pytest.mark.parametrize(
420-
"dtype,close_dtype",
421-
[
422-
(np.int64, np.int32), (np.uint64, np.uint32),
423-
(np.float64, np.float32), (np.complex128, np.complex64)
424-
]
425-
)
426-
@pytest.mark.parametrize(
427-
"dtype_group",
428-
[
429-
None, "signed integer", "unsigned integer", "integral",
430-
"real floating", "complex floating", "numeric"
431-
]
432-
)
433421
class TestIsDType:
434422
"""
435423
Check correctness of `np.isdtype`. The test considers different argument
@@ -448,6 +436,20 @@ class TestIsDType:
448436
)
449437
}
450438

439+
@pytest.mark.parametrize(
440+
"dtype,close_dtype",
441+
[
442+
(np.int64, np.int32), (np.uint64, np.uint32),
443+
(np.float64, np.float32), (np.complex128, np.complex64)
444+
]
445+
)
446+
@pytest.mark.parametrize(
447+
"dtype_group",
448+
[
449+
None, "signed integer", "unsigned integer", "integral",
450+
"real floating", "complex floating", "numeric"
451+
]
452+
)
451453
def test_isdtype(self, dtype, close_dtype, dtype_group):
452454
# First check if same dtypes return `true` and different ones
453455
# give `false` (even if they're close in the dtype hierarchy!)
@@ -464,6 +466,12 @@ def test_isdtype(self, dtype, close_dtype, dtype_group):
464466
else:
465467
assert not np.isdtype(dtype, dtype_group)
466468

469+
def test_isdtype_invalid_args(self):
470+
with assert_raises_regex(TypeError, r".*must be a dtype.*"):
471+
np.isdtype("int64", np.int64)
472+
with assert_raises_regex(TypeError, r".*kind argument must.*"):
473+
np.isdtype(np.int64, "int64")
474+
467475

468476
class TestSctypeDict:
469477
def test_longdouble(self):

0 commit comments

Comments
 (0)