Skip to content

Commit

Permalink
API: Remove zero names from dtype aliases (#24807)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsokol committed Sep 29, 2023
1 parent f2a9b50 commit 241d676
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
12 changes: 6 additions & 6 deletions numpy/_typing/_char_codes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Literal

_BoolCodes = Literal["?", "=?", "<?", ">?", "bool", "bool_", "bool8"]
_BoolCodes = Literal["?", "=?", "<?", ">?", "bool", "bool_"]

_UInt8Codes = Literal["uint8", "u1", "=u1", "<u1", ">u1"]
_UInt16Codes = Literal["uint16", "u2", "=u2", "<u2", ">u2"]
Expand All @@ -22,14 +22,14 @@
_ByteCodes = Literal["byte", "b", "=b", "<b", ">b"]
_ShortCodes = Literal["short", "h", "=h", "<h", ">h"]
_IntCCodes = Literal["intc", "i", "=i", "<i", ">i"]
_IntPCodes = Literal["intp", "int0", "p", "=p", "<p", ">p"]
_IntPCodes = Literal["intp", "p", "=p", "<p", ">p"]
_IntCodes = Literal["long", "int", "int_", "l", "=l", "<l", ">l"]
_LongLongCodes = Literal["longlong", "q", "=q", "<q", ">q"]

_UByteCodes = Literal["ubyte", "B", "=B", "<B", ">B"]
_UShortCodes = Literal["ushort", "H", "=H", "<H", ">H"]
_UIntCCodes = Literal["uintc", "I", "=I", "<I", ">I"]
_UIntPCodes = Literal["uintp", "uint0", "P", "=P", "<P", ">P"]
_UIntPCodes = Literal["uintp", "P", "=P", "<P", ">P"]
_UIntCodes = Literal["ulong", "uint", "L", "=L", "<L", ">L"]
_ULongLongCodes = Literal["ulonglong", "Q", "=Q", "<Q", ">Q"]

Expand All @@ -42,9 +42,9 @@
_CDoubleCodes = Literal["cdouble", "complex", "D", "=D", "<D", ">D"]
_CLongDoubleCodes = Literal["clongdouble", "G", "=G", "<G", ">G"]

_StrCodes = Literal["str", "str_", "str0", "unicode", "U", "=U", "<U", ">U"]
_BytesCodes = Literal["bytes", "bytes_", "bytes0", "S", "=S", "<S", ">S"]
_VoidCodes = Literal["void", "void0", "V", "=V", "<V", ">V"]
_StrCodes = Literal["str", "str_", "unicode", "U", "=U", "<U", ">U"]
_BytesCodes = Literal["bytes", "bytes_", "S", "=S", "<S", ">S"]
_VoidCodes = Literal["void", "V", "=V", "<V", ">V"]
_ObjectCodes = Literal["object", "object_", "O", "=O", "<O", ">O"]

_DT64Codes = Literal[
Expand Down
12 changes: 4 additions & 8 deletions numpy/core/_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,15 @@ def _add_aliases():
if name in ('longdouble', 'clongdouble') and myname in allTypes:
continue

# Add to the main namespace if desired:
if bit != 0 and base != "bool":
# add to the main namespace
allTypes[myname] = info.type
# add mapping for both the bit name
sctypeDict[myname] = info.type

# add forward, reverse, and string mapping to numarray
sctypeDict[char] = info.type

# add mapping for both the bit name
sctypeDict[myname] = info.type


_add_aliases()

def _add_integer_aliases():
Expand Down Expand Up @@ -224,9 +222,7 @@ def _set_array_types():

# Add additional strings to the sctypeDict
_toadd = ['int', ('float', 'double'), ('complex', 'cdouble'),
'bool', 'object',
'str', 'bytes', ('a', 'bytes_'),
('int0', 'intp'), ('uint0', 'uintp')]
'bool', 'object', 'str', 'bytes', ('a', 'bytes_')]

for name in _toadd:
if isinstance(name, tuple):
Expand Down
11 changes: 11 additions & 0 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,17 @@ _convert_from_str(PyObject *obj, int align)
if (PyErr_Occurred()) {
return NULL;
}
if (
strcmp(type, "int0") == 0 || strcmp(type, "uint0") == 0 ||
strcmp(type, "void0") == 0 || strcmp(type, "object0") == 0 ||
strcmp(type, "str0") == 0 || strcmp(type, "bytes0") == 0 ||
strcmp(type, "bool8") == 0
) {
PyErr_Format(PyExc_TypeError,
"Alias %R was removed in NumPy 2.0. Use a name "
"without a digit at the end.", obj);
return NULL;
}
goto fail;
}

Expand Down
24 changes: 16 additions & 8 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,22 @@ def test_numeric_style_types_are_invalid(self, dtype):
with assert_raises(TypeError):
np.dtype(dtype)

def test_remaining_dtypes_with_bad_bytesize(self):
# The np.<name> aliases were deprecated, these probably should be too
assert np.dtype("int0") is np.dtype("intp")
assert np.dtype("uint0") is np.dtype("uintp")
assert np.dtype("bool8") is np.dtype("bool")
assert np.dtype("bytes0") is np.dtype("bytes")
assert np.dtype("str0") is np.dtype("str")
assert np.dtype("object0") is np.dtype("object")
def test_expired_dtypes_with_bad_bytesize(self):
match: str = r".*removed in NumPy 2.0.*"
with pytest.raises(TypeError, match=match):
np.dtype("int0")
with pytest.raises(TypeError, match=match):
np.dtype("uint0")
with pytest.raises(TypeError, match=match):
np.dtype("bool8")
with pytest.raises(TypeError, match=match):
np.dtype("bytes0")
with pytest.raises(TypeError, match=match):
np.dtype("str0")
with pytest.raises(TypeError, match=match):
np.dtype("object0")
with pytest.raises(TypeError, match=match):
np.dtype("void0")

@pytest.mark.parametrize(
'value',
Expand Down

0 comments on commit 241d676

Please sign in to comment.