8000 DEP: Make issubdtype consistent for types and dtypes · seberg/numpy@0769f8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0769f8c

Browse files
committed
DEP: Make issubdtype consistent for types and dtypes
This finishes the deprecation started in numpygh-9505 removing behaviour that allowed strings/types representing specific dtypes to behave like their more generic supertypes (e.g. the python float would map to floating instead of float64 which it typically maps to).
1 parent 4f2b219 commit 0769f8c

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``issubdtype`` more consistent/restrictive
2+
----------------------------------------------
3+
4+
`numpy.issubdtype` had a FutureWarning since NumPy 1.14 which
5+
has expired now. This means that certain input where the second
6+
argument was neither a datatype nor a NumPy scalar type
7+
(such as a string or a python type like ``int`` or ``float``)
8+
will now be consistent with passing in ``np.dtype(arg2).type``.
9+
This makes the result consistent with expectations and leads to
10+
a false result in some cases which previously returned true.

numpy/core/numerictypes.py

Lines changed: 0 additions & 28 deletions
< 10000 td data-grid-cell-id="diff-4320badc86198da84303c9c27807bfba2c3bd82d4213a1660ce8a76b71baa96a-394-391-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">394
Original file line numberDiff line numberDiff line change
@@ -388,35 +388,7 @@ def issubdtype(arg1, arg2):
388388
if not issubclass_(arg1, generic):
389389
arg1 = dtype(arg1).type
390390
if not issubclass_(arg2, generic):
391-
arg2_orig = arg2
392391
arg2 = dtype(arg2).type
393-
if not isinstance(arg2_orig, dtype):
-
# weird deprecated behaviour, that tried to infer np.floating from
395-
# float, and similar less obvious things, such as np.generic from
396-
# str.
397-
mro = arg2.mro()
398-
arg2 = mro[1] if len(mro) > 1 else mro[0]
399-
400-
def type_repr(x):
401-
""" Helper to produce clear error messages """
402-
if not isinstance(x, type):
403-
return repr(x)
404-
elif issubclass(x, generic):
405-
return "np.{}".format(x.__name__)
406-
else:
407-
return x.__name__
408-
409-
# 1.14, 2017-08-01
410-
warnings.warn(
411-
"Conversion of the second argument of issubdtype from `{raw}` "
412-
"to `{abstract}` is deprecated. In future, it will be treated "
413-
"as `{concrete} == np.dtype({raw}).type`.".format(
414-
raw=type_repr(arg2_orig),
415-
abstract=type_repr(arg2),
416-
concrete=type_repr(dtype(arg2_orig).type)
417-
),
418-
FutureWarning, stacklevel=2
419-
)
420392

421393
return issubclass(arg1, arg2)
422394

numpy/core/tests/test_numerictypes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,35 @@ def test_sibling_class(self):
401401
assert_(not np.issubdtype(w1(np.float32), w2(np.float64)))
402402
assert_(not np.issubdtype(w1(np.float64), w2(np.float32)))
403403

404+
def test_nondtype_nonscalartype(self):
405+
# See gh-14619 and gh-9505 which introduced the deprecation to fix
406+
# this. These tests are directly taken from gh-9505
407+
assert not np.issubdtype(np.float32, 'float64')
408+
assert not np.issubdtype(np.float32, 'f8')
409+
assert not np.issubdtype(np.int32, str)
410+
assert not np.issubdtype(np.int32, 'int64')
411+
assert not np.issubdtype(np.str_, 'void')
412+
# for the following the correct spellings are
413+
# np.integer, np.floating, or np.complexfloating respectively:
414+
assert not np.issubdtype(np.int32, int)
415+
assert not np.issubdtype(np.float32, float)
416+
assert not np.issubdtype(np.complex64, complex)
417+
assert not np.issubdtype(np.float32, "float")
418+
assert not np.issubdtype(np.float64, "f")
419+
420+
# Test the same for the correct first datatype and abstract one
421+
# in the case of int, float, complex:
422+
assert np.issubdtype(np.float64, 'float64')
423+
assert np.issubdtype(np.float64, 'f8')
424+
assert np.issubdtype(np.str_, str)
425+
assert np.issubdtype(np.int64, 'int64')
426+
assert np.issubdtype(np.void, 'void')
427+
assert np.issubdtype(np.int32, np.integer)
428+
assert np.issubdtype(np.float32, np.floating)
429+
assert np.issubdtype(np.complex64, np.complexfloating)
430+
assert np.issubdtype(np.float64, "float")
431+
assert np.issubdtype(np.float32, "f")
432+
404433

405434
class TestSctypeDict:
406435
def test_longdouble(self):

0 commit comments

Comments
 (0)
0