Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 85cae50

Browse filesBrowse files
committed
Apply review comments
1 parent 4d1b7a8 commit 85cae50
Copy full SHA for 85cae50

File tree

4 files changed

+52
-36
lines changed
Filter options

4 files changed

+52
-36
lines changed
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
* ``arr.T`` property has been deprecated for array scalars and arrays with dimensionality
2-
different than ``2`` to be compatible with the Array API standard. To achieve the same
3-
behavior when ``arr.ndim != 2``, either ``np.permute_dims(arr, range(arr.ndim)[::-1])``
4-
(also compatible with the Array API) or ``arr.transpose()`` can be used.
2+
different than ``2`` to be compatible with the Array API standard. To achieve similar
3+
behavior when ``arr.ndim != 2``, either ``arr.transpose()``, or ``arr.mT`` (swaps
4+
the last two axes only), or ``np.permute_dims(arr, range(arr.ndim)[::-1])`` (compatible
5+
with the Array API) or can be used.

‎numpy/_core/src/multiarray/getset.c

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/getset.c
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,13 @@ array_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
850850
{
851851
int ndim = PyArray_NDIM(self);
852852
if (ndim != 2) {
853-
if (PyErr_WarnFormat(PyExc_UserWarning, 1,
853+
/* Deprecated 2025-04-19, NumPy 2.3 */
854+
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
854855
"In the future `.T` property will be supported for "
855856
"2-dim arrays only. Received %d-dim array. Either "
856-
"`np.permute_dims(arr, range(arr.ndim)[::-1])` "
857-
"(compatible with the Array API) or `arr.transpose()` "
858-
"should be used instead.",
859-
ndim) < 0) {
857+
"`arr.transpose()` or `.mT` (which swaps the last "
858+
"two axes only) should be used instead."
859+
"(Deprecated NumPy 2.3)", ndim) < 0) {
860860
return NULL;
861861
}
862862
}

‎numpy/_core/src/multiarray/scalartypes.c.src

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/scalartypes.c.src
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,13 +1910,14 @@ gentype_flat_get(PyObject *self, void *NPY_UNUSED(ignored))
19101910
static PyObject *
19111911
gentype_transpose_get(PyObject *self, void *NPY_UNUSED(ignored))
19121912
{
1913-
if (PyErr_WarnEx(PyExc_UserWarning,
1913+
/* Deprecated 2025-04-19, NumPy 2.3 */
1914+
if (DEPRECATE(
19141915
"In the future `.T` property for array scalars will "
19151916
"raise an error. If you call `.T` on an array scalar "
19161917
"intentionally you can safely drop it. In other cases "
1917-
"`np.permute_dims(arr, range(arr.ndim)[::-1])` "
1918-
"(compatible with the Array API) or `arr.transpose()` "
1919-
"should be used instead.", 1) < 0) {
1918+
"`arr.transpose()` or `.mT` (which swaps the last "
1919+
"two axes only) should be used instead. "
1920+
"(Deprecated NumPy 2.3)") < 0) {
19201921
return NULL;
19211922
}
19221923
Py_INCREF(self);

‎numpy/_core/tests/test_deprecations.py

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_deprecations.py
+38-24Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@
55
"""
66
import warnings
77
import pytest
8-
import tempfile
98
import re
109

1110
import numpy as np
12-
from numpy.testing import (
13-
assert_raises, assert_warns, assert_, assert_array_equal, SkipTest,
14-
KnownFailureException, break_cycles, temppath
15-
)
11+
from numpy.testing import assert_raises, temppath
1612

17-
from numpy._core._multiarray_tests import fromstring_null_term_c_api
1813
import numpy._core._struct_ufunc_tests as struct_ufunc
1914

20-
try:
21-
import pytz
22-
_has_pytz = True
23-
except ImportError:
24-
_has_pytz = False
25-
2615

2716
class _DeprecationTestCase:
2817
# Just as warning: warnings uses re.match, so the start of this message
@@ -47,7 +36,8 @@ def setup_method(self):
4736
def teardown_method(self):
4837
self.warn_ctx.__exit__()
4938

50-
def assert_deprecated(self, function, num=1, ignore_others=False,
39+
def assert_deprecated(self, function, num=1, msg_patterns=None,
40+
ignore_others=False,
5141
function_fails=False,
5242
exceptions=np._NoValue,
5343
args=(), kwargs={}):
@@ -65,6 +55,11 @@ def assert_deprecated(self, function, num=1, ignore_others=False,
6555
The function to test
6656
num : int
6757
Number of DeprecationWarnings to expect. This should normally be 1.
58+
msg_patterns : str or tuple of str
59+
Patterns for which warning messages should match. For `str` each
60+
warning should match to the same pattern. For a tuple of `str`
61+
each warning should match against the corresponding pattern.
62+
For `None` this check is skipped.
6863
ignore_others : bool
6964
Whether warnings of the wrong type should be ignored (note that
7065
the message is not checked)
@@ -96,6 +91,14 @@ def assert_deprecated(self, function, num=1, ignore_others=False,
9691
# just in case, clear the registry
9792
num_found = 0
9893
for warning in self.log:
94+
if msg_patterns is not None:
95+
pattern = (msg_patterns if isinstance(msg_patterns, str) else
96+
msg_patterns[num_found])
97+
msg = warning.message.args[0]
98+
if re.match(pattern, msg) is None:
99+
raise AssertionError(
100+
"expected %s warning message pattern but got: %s" %
101+
(pattern, msg))
99102
if warning.category is self.warning_cls:
100103
num_found += 1
101104
elif not ignore_others:
@@ -145,9 +148,17 @@ def test_assert_deprecated(self):
145148
lambda: None)
146149

147150
def foo():
151+
warnings.warn("foo bar", category=DeprecationWarning,
152+
stacklevel=2)
153+
154+
def foo_many():
148155
warnings.warn("foo", category=DeprecationWarning, stacklevel=2)
156+
warnings.warn("bar", category=DeprecationWarning, stacklevel=2)
149157

150158
test_case_instance.assert_deprecated(foo)
159+
test_case_instance.assert_deprecated(foo, msg_patterns="foo")
160+
test_case_instance.assert_deprecated(foo_many, num=2,
161+
msg_patterns=("foo", "^bar$"))
151162
test_case_instance.teardown_method()
152163

153164

@@ -456,15 +467,18 @@ def test_deprecated(self):
456467
)
457468

458469

459-
def test_deprecated_T_non_2dim():
470+
class TestDeprecatedTNon2Dim(_DeprecationTestCase):
460471
# Deprecated in Numpy 2.3, 2025-04
461-
with pytest.warns(UserWarning, match="In the future `.T` property for "
462-
"array scalars will raise an error."):
463-
np.int64(1).T
464-
for shape in [(5,), (2, 3, 4)]:
465-
with pytest.warns(
466-
UserWarning,
467-
match="In the future `.T` property will be "
468-
"supported for 2-dim arrays only. "
469-
f"Received {len(shape)}-dim array."):
470-
np.ones(shape).T
472+
def test_deprecated(self):
473+
self.assert_deprecated(
474+
lambda: np.int64(1).T,
475+
msg_patterns="In the future `.T` property for "
476+
"array scalars will raise an error."
477+
)
478+
for shape in [(5,), (2, 3, 4)]:
479+
self.assert_deprecated(
480+
lambda: np.ones(shape).T,
481+
msg_patterns="In the future `.T` property will be "
482+
"supported for 2-dim arrays only. "
483+
f"Received {len(shape)}-dim array."
484+
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.