8000 Merge pull request #26524 from ngoldbaum/fix-dtype-a · numpy/numpy@5807561 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5807561

Browse files
authored
Merge pull request #26524 from ngoldbaum/fix-dtype-a
BUG: fix incorrect error handling for dtype('a') deprecation
2 parents 59e0db9 + e7da8d9 commit 5807561

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

numpy/_core/src/multiarray/conversion_utils.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,13 +1341,19 @@ PyArray_TypestrConvert(int itemsize, int gentype)
13411341
break;
13421342

13431343
case NPY_DEPRECATED_STRINGLTR2:
1344-
DEPRECATE(
1345-
"Data type alias `a` was removed in NumPy 2.0. "
1346-
"Use `S` alias instead."
1347-
);
1348-
newtype = NPY_STRING;
1344+
{
1345+
/*
1346+
* raise a deprecation warning, which might be an exception
1347+
* if warnings are errors, so leave newtype unset in that
1348+
* case
1349+
*/
1350+
int ret = DEPRECATE("Data type alias 'a' was deprecated in NumPy 2.0. "
1351+
"Use the 'S' alias instead.");
1352+
if (ret == 0) {
1353+
newtype = NPY_STRING;
1354+
}
13491355
break;
1350-
1356+
}
13511357
case NPY_UNICODELTR:
13521358
newtype = NPY_UNICODE;
13531359
break;

numpy/_core/src/multiarray/descriptor.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,10 +1828,10 @@ _convert_from_str(PyObject *obj, int align)
18281828
break;
18291829

18301830
case NPY_DEPRECATED_STRINGLTR2:
1831-
DEPRECATE(
1832-
"Data type alias `a` was removed in NumPy 2.0. "
1833-
"Use `S` alias instead."
1834-
);
1831+
if (DEPRECATE("Data type alias 'a' was deprecated in NumPy 2.0. "
1832+
"Use the 'S' alias instead.") < 0) {
1833+
return NULL;
1834+
}
18351835
check_num = NPY_STRING;
18361836
break;
18371837

@@ -1900,10 +1900,10 @@ _convert_from_str(PyObject *obj, int align)
19001900
}
19011901

19021902
if (strcmp(type, "a") == 0) {
1903-
DEPRECATE(
1904-
"Data type alias `a` was removed in NumPy 2.0. "
1905-
"Use `S` alias instead."
1906-
);
1903+
if (DEPRECATE("Data type alias 'a' was deprecated in NumPy 2.0. "
1904+
"Use the 'S' alias instead.") < 0) {
1905+
return NULL;
1906+
}
19071907
}
19081908

19091909
/*

numpy/_core/tests/test_deprecations.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,22 @@ def test_lib_functions_deprecation_call(self):
670670

671671
class TestDeprecatedDTypeAliases(_DeprecationTestCase):
672672

673-
@staticmethod
674-
def _check_for_warning(func):
673+
def _check_for_warning(self, func):
675674
with warnings.catch_warnings(record=True) as caught_warnings:
676675
func()
677676
assert len(caught_warnings) == 1
678677
w = caught_warnings[0]
679678
assert w.category is DeprecationWarning
680-
assert "alias `a` was removed in NumPy 2.0" in str(w.message)
679+
assert "alias 'a' was deprecated in NumPy 2.0" in str(w.message)
681680

682681
def test_a_dtype_alias(self):
683-
self._check_for_warning(lambda: np.dtype("a"))
684-
self._check_for_warning(lambda: np.dtype("a10"))
682+
for dtype in ["a", "a10"]:
683+
f = lambda: np.dtype(dtype)
684+
self._check_for_warning(f)
685+
self.assert_deprecated(f)
686+
f = lambda: np.array(["hello", "world"]).astype("a10")
687+
self._check_for_warning(f)
688+
self.assert_deprecated(f)
685689

686690

687691
class TestDeprecatedArrayWrap(_DeprecationTestCase):

0 commit comments

Comments
 (0)
0