Skip to content

Commit

Permalink
make sure that error message is only modified for string kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis committed May 16, 2024
1 parent 2a79cee commit a74f7fa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions numpy/_core/numerictypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def issubsctype(arg1, arg2):
return issubclass(obj2sctype(arg1), obj2sctype(arg2))


def _preprocess_dtype(dtype, err_msg):
def _preprocess_dtype(dtype, err_msg, is_kind=False):
"""
Preprocess dtype argument by:
1. fetching type from a data type
Expand All @@ -369,8 +369,11 @@ def _preprocess_dtype(dtype, err_msg):
if isinstance(dtype, ma.dtype):
dtype = dtype.type
if isinstance(dtype, ndarray) or dtype not in allTypes.values():
if isinstance(dtype, str):
message = f"{err_msg}, but {repr(dtype)} is not a valid kind name."
if is_kind and isinstance(dtype, str):
message = (
"kind argument is a string, but"
f" {repr(dtype)} is not a known kind name."
)
else:
message = f"{err_msg}, but it is a {type(dtype)}."
raise TypeError(message)
Expand Down Expand Up @@ -449,6 +452,7 @@ def isdtype(dtype, kind):
kind,
err_msg="kind argument must be comprised of "
"NumPy dtypes or strings only"
is_kind=True,
)
processed_kinds.add(kind)

Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/tests/test_numerictypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def test_isdtype_invalid_args(self):
np.isdtype("int64", np.int64)
with assert_raises_regex(TypeError, r".*kind argument must.*"):
np.isdtype(np.int64, 1)
with assert_raises_regex(TypeError, r".*is not a valid name.*"):
with assert_raises_regex(TypeError, r".*is not a known kind name.*"):
np.isdtype(np.int64, "int64")

def test_sctypes_complete(self):
Expand Down

0 comments on commit a74f7fa

Please sign in to comment.