8000 BUG: fix incorrect error handling for dtype('a') deprecation by ngoldbaum · Pull Request #26524 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fix incorrect error handling for dtype('a') deprecation #26524

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 1 commit into from
May 24, 2024
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
18 changes: 12 additions & 6 deletions numpy/_core/src/multiarray/conversion_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1341,13 +1341,19 @@ PyArray_TypestrConvert(int itemsize, int gentype)
break;

case NPY_DEPRECATED_STRINGLTR2:
DEPRECATE(
"Data type alias `a` was removed in NumPy 2.0. "
"Use `S` alias instead."
);
newtype = NPY_STRING;
{
/*
* raise a deprecation warning, which might be an exception
* if warnings are errors, so leave newtype unset in that
* case
*/
int ret = DEPRECATE("Data type alias 'a' was deprecated in NumPy 2.0. "
"Use the 'S' alias instead.");
if (ret == 0) {
newtype = NPY_STRING;
}
break;

}
case NPY_UNICODELTR:
newtype = NPY_UNICODE;
break;
Expand Down
16 changes: 8 additions & 8 deletions numpy/_core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1828,10 +1828,10 @@ _convert_from_str(PyObject *obj, int align)
break;

case NPY_DEPRECATED_STRINGLTR2:
DEPRECATE(
"Data type alias `a` was removed in NumPy 2.0. "
"Use `S` alias instead."
);
if (DEPRECATE("Data type alias 'a' was deprecated in NumPy 2.0. "
"Use the 'S' alias instead.") < 0) {
return NULL;
}
check_num = NPY_STRING;
break;

Expand Down Expand Up @@ -1900,10 +1900,10 @@ _convert_from_str(PyObject *obj, int align)
}

if (strcmp(type, "a") == 0) {
DEPRECATE(
"Data type alias `a` was removed in NumPy 2.0. "
"Use `S` alias instead."
);
if (DEPRECATE("Data type alias 'a' was deprecated in NumPy 2.0. "
"Use the 'S' alias instead.") < 0) {
return NULL;
}
}

/*
Expand Down
14 changes: 9 additions & 5 deletions numpy/_core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,22 @@ def test_lib_functions_deprecation_call(self):

class TestDeprecatedDTypeAliases(_DeprecationTestCase):

@staticmethod
def _check_for_warning(func):
def _check_for_warning(self, func):
with warnings.catch_warnings(record=True) as caught_warnings:
func()
assert len(caught_warnings) == 1
w = caught_warnings[0]
assert w.category is DeprecationWarning
assert "alias `a` was removed in NumPy 2.0" in str(w.message)
assert "alias 'a' was deprecated in NumPy 2.0" in str(w.message)

def test_a_dtype_alias(self):
self._check_for_warning(lambda: np.dtype("a"))
self._check_for_warning(lambda: np.dtype("a10"))
for dtype in ["a", "a10"]:
f = lambda: np.dtype(dtype)
self._check_for_warning(f)
self.assert_deprecated(f)
f = lambda: np.array(["hello", "world"]).astype("a10")
self._check_for_warning(f)
self.assert_deprecated(f)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the quick fix! It doesn't matter, just FYI: assert_deprecated checks for both the warning and the error path, all that is needed to make it specific is setting message = ... as a class variable.



class TestDeprecatedArrayWrap(_DeprecationTestCase):
Expand Down
Loading
0