Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Remove zero names from dtype aliases #24807

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_')]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.dtype('a') also seems ripe for removal, never seen it before and looks very odd. Does anyone know where that came from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually noticed np.dtype('a') but I couldn't figure out its origin and purpose 😄 - therefore I decided to keep it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this goes back to 9ac3b0e. I have no idea why Travis added it and I don't know whether issues from the old trac issue tracker are still visible somewhere. Github search only finds 12 code usages of "np.dtype('a')". I say remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably it is meant to mean "ascii"... if almost nobody uses, it shouldn't matter, unlik "c" it dosn't eve have a special meaning.


for name in _toadd:
if isinstance(name, tuple):
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,14 @@ _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
) {
goto expired_alias_fail;
rgommers marked this conversation as resolved.
Show resolved Hide resolved
}
goto fail;
}

Expand Down Expand Up @@ -1874,6 +1882,10 @@ _convert_from_str(PyObject *obj, int align)
}
return ret;

expired_alias_fail:
PyErr_Format(PyExc_TypeError, "Alias %R was removed in NumPy 2.0. Use "
"a name without a digit at the end.", obj);
return NULL;
fail:
PyErr_Format(PyExc_TypeError, "data type %R not understood", obj);
return NULL;
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