Skip to content

MAINT: Remove 'a' dtype alias #24854

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

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/release/upcoming_changes/24854.python_removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``np.dtype("a")`` alias for ``np.dtype(np.bytes_)`` was removed.
Use ``np.dtype("S")`` alias instead.
4 changes: 2 additions & 2 deletions doc/source/reference/arrays.dtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Array-protocol type strings (see :ref:`arrays.interface`)
>>> dt = np.dtype('i4') # 32-bit signed integer
>>> dt = np.dtype('f8') # 64-bit floating-point number
>>> dt = np.dtype('c16') # 128-bit complex floating-point number
>>> dt = np.dtype('a25') # 25-length zero-terminated bytes
>>> dt = np.dtype('S25') # 25-length zero-terminated bytes
>>> dt = np.dtype('U25') # 25-character string

.. _string-dtype-note:
Expand Down Expand Up @@ -293,7 +293,7 @@ String with comma-separated fields
- field named ``f2`` containing a 3 x 4 sub-array
containing 10-character strings

>>> dt = np.dtype("a3, 3u8, (3,4)a10")
>>> dt = np.dtype("S3, 3u8, (3,4)S10")

Type strings
Any string name of a NumPy dtype, e.g.:
Expand Down
2 changes: 1 addition & 1 deletion doc/source/user/basics.rec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ A record array representation of a structured array can be obtained using the
appropriate `view <numpy-ndarray-view>`_::

>>> arr = np.array([(1, 2., 'Hello'), (2, 3., "World")],
... dtype=[('foo', 'i4'),('bar', 'f4'), ('baz', 'a10')])
... dtype=[('foo', 'i4'),('bar', 'f4'), ('baz', 'S10')])
>>> recordarr = arr.view(dtype=np.dtype((np.record, arr.dtype)),
... type=np.recarray)

Expand Down
2 changes: 1 addition & 1 deletion numpy/core/_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _set_array_types():

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

for name in _toadd:
if isinstance(name, tuple):
Expand Down
1 change: 0 additions & 1 deletion numpy/core/include/numpy/ndarraytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ enum NPY_TYPECHAR {
NPY_CLONGDOUBLELTR = 'G',
NPY_OBJECTLTR = 'O',
NPY_STRINGLTR = 'S',
NPY_STRINGLTR2 = 'a',
NPY_UNICODELTR = 'U',
NPY_VOIDLTR = 'V',
NPY_DATETIMELTR = 'M',
Expand Down
2 changes: 0 additions & 2 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -4724,8 +4724,6 @@ set_typeinfo(PyObject *dict)

/**end repeat**/

_letter_to_num[NPY_STRINGLTR2] = NPY_STRING;

/**begin repeat
* #name = BOOL,
* BYTE, UBYTE, SHORT, USHORT, INT, UINT,
Expand Down
1 change: 0 additions & 1 deletion numpy/core/src/multiarray/conversion_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,6 @@ PyArray_TypestrConvert(int itemsize, int gentype)
break;

case NPY_STRINGLTR:
case NPY_STRINGLTR2:
newtype = NPY_STRING;
break;

Expand Down
1 change: 0 additions & 1 deletion numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,6 @@ _convert_from_str(PyObject *obj, int align)
kind = type[0];
switch (kind) {
case NPY_STRINGLTR:
case NPY_STRINGLTR2:
check_num = NPY_STRING;
break;

Expand Down
2 changes: 1 addition & 1 deletion numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_expired_dtypes_with_bad_bytesize(self):
@pytest.mark.parametrize(
'value',
['m8', 'M8', 'datetime64', 'timedelta64',
'i4, (2,3)f8, f4', 'a3, 3u8, (3,4)a10',
'i4, (2,3)f8, f4', 'S3, 3u8, (3,4)S10',
'>f', '<f', '=f', '|f',
])
def test_dtype_bytes_str_equivalence(self, value):
Expand Down
34 changes: 24 additions & 10 deletions numpy/core/tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,29 @@ def test_fromrecords_2d(self):
assert_equal(r1, r2)

def test_method_array(self):
r = np.rec.array(b'abcdefg' * 100, formats='i2,a3,i4', shape=3, byteorder='big')
r = np.rec.array(
b'abcdefg' * 100, formats='i2,S3,i4', shape=3, byteorder='big'
)
assert_equal(r[1].item(), (25444, b'efg', 1633837924))

def test_method_array2(self):
r = np.rec.array([(1, 11, 'a'), (2, 22, 'b'), (3, 33, 'c'), (4, 44, 'd'), (5, 55, 'ex'),
(6, 66, 'f'), (7, 77, 'g')], formats='u1,f4,a1')
r = np.rec.array(
[
(1, 11, 'a'), (2, 22, 'b'), (3, 33, 'c'), (4, 44, 'd'),
(5, 55, 'ex'), (6, 66, 'f'), (7, 77, 'g')
],
formats='u1,f4,S1'
)
assert_equal(r[1].item(), (2, 22.0, b'b'))

def test_recarray_slices(self):
r = np.rec.array([(1, 11, 'a'), (2, 22, 'b'), (3, 33, 'c'), (4, 44, 'd'), (5, 55, 'ex'),
(6, 66, 'f'), (7, 77, 'g')], formats='u1,f4,a1')
r = np.rec.array(
[
(1, 11, 'a'), (2, 22, 'b'), (3, 33, 'c'), (4, 44, 'd'),
(5, 55, 'ex'), (6, 66, 'f'), (7, 77, 'g')
],
formats='u1,f4,S1'
)
assert_equal(r[1::2][1].item(), (4, 44.0, b'd'))

def test_recarray_fromarrays(self):
Expand All @@ -78,14 +90,16 @@ def test_recarray_fromfile(self):
filename = path.join(data_dir, 'recarray_from_file.fits')
fd = open(filename, 'rb')
fd.seek(2880 * 2)
r1 = np.rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big')
r1 = np.rec.fromfile(fd, formats='f8,i4,S5', shape=3, byteorder='big')
fd.seek(2880 * 2)
r2 = np.rec.array(fd, formats='f8,i4,a5', shape=3, byteorder='big')
r2 = np.rec.array(fd, formats='f8,i4,S5', shape=3, byteorder='big')
fd.seek(2880 * 2)
bytes_array = BytesIO()
bytes_array.write(fd.read())
bytes_array.seek(0)
r3 = np.rec.fromfile(bytes_array, formats='f8,i4,a5', shape=3, byteorder='big')
r3 = np.rec.fromfile(
bytes_array, formats='f8,i4,S5', shape=3, byteorder='big'
)
fd.close()
assert_equal(r1, r2)
assert_equal(r2, r3)
Expand Down Expand Up @@ -330,12 +344,12 @@ def test_tofile_fromfile(self):
with temppath(suffix='.bin') as path:
path = Path(path)
np.random.seed(123)
a = np.random.rand(10).astype('f8,i4,a5')
a = np.random.rand(10).astype('f8,i4,S5')
a[5] = (0.5,10,'abcde')
with path.open("wb") as fd:
a.tofile(fd)
x = np.core.records.fromfile(path,
formats='f8,i4,a5',
formats='f8,i4,S5',
shape=10)
assert_array_equal(x, a)

Expand Down
2 changes: 1 addition & 1 deletion numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def test_rec_fromarray(self):
x1 = np.array([[1, 2], [3, 4], [5, 6]])
x2 = np.array(['a', 'dd', 'xyz'])
x3 = np.array([1.1, 2, 3])
np.rec.fromarrays([x1, x2, x3], formats="(2,)i4,a3,f8")
np.rec.fromarrays([x1, x2, x3], formats="(2,)i4,S3,f8")

def test_object_array_assign(self):
x = np.empty((2, 2), object)
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/tests/test_return_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def check_function(self, t, tname):
assert abs(t(array([234 + 3j], "F")) - (234 + 3j)) <= err
assert abs(t(array([234], "D")) - 234.0) <= err

# pytest.raises(TypeError, t, array([234], 'a1'))
# pytest.raises(TypeError, t, array([234], 'S1'))
pytest.raises(TypeError, t, "abc")

pytest.raises(IndexError, t, [])
Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def test_index_array_copied(self):

def test_structured_array(self):
a = np.array([(1, 'a'), (2, 'b'), (3, 'c')],
dtype=[('foo', 'i'), ('bar', 'a1')])
dtype=[('foo', 'i'), ('bar', 'S1')])
val = (4, 'd')
b = np.insert(a, 0, val)
assert_array_equal(b[0], np.array(val, dtype=b.dtype))
Expand Down
1 change: 0 additions & 1 deletion tools/replace_old_macros.sed
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ s/\bPyArray_CDOUBLELTR\b/NPY_CDOUBLELTR/g
s/\bPyArray_CLONGDOUBLELTR\b/NPY_CLONGDOUBLELTR/g
s/\bPyArray_OBJECTLTR\b/NPY_OBJECTLTR/g
s/\bPyArray_STRINGLTR\b/NPY_STRINGLTR/g
s/\bPyArray_STRINGLTR2\b/NPY_STRINGLTR2/g
s/\bPyArray_UNICODELTR\b/NPY_UNICODELTR/g
s/\bPyArray_VOIDLTR\b/NPY_VOIDLTR/g
s/\bPyArray_DATETIMELTR\b/NPY_DATETIMELTR/g
Expand Down