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

DEP: Deprecate .T property for non-2dim arrays and scalars #28678

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 4 commits into from
May 16, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Apply review comments
  • Loading branch information
mtsokol committed May 16, 2025
commit 13b5692a2df80fe8e7568340b07d2bc58c87fc45
2 changes: 1 addition & 1 deletion 2 doc/release/upcoming_changes/28678.deprecation.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* ``arr.T`` property has been deprecated for array scalars and arrays with dimensionality
* The ``arr.T`` property has been deprecated for array scalars and arrays with dimensionality
different than ``2`` to be compatible with the Array API standard. To achieve similar
behavior when ``arr.ndim != 2``, either ``arr.transpose()``, or ``arr.mT`` (swaps
the last two axes only), or ``np.permute_dims(arr, range(arr.ndim)[::-1])`` (compatible
Expand Down
8 changes: 4 additions & 4 deletions 8 numpy/_core/src/multiarray/getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,10 @@ array_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
if (ndim != 2) {
seberg marked this conversation as resolved.
Show resolved Hide resolved
/* Deprecated 2025-04-19, NumPy 2.3 */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"In the future `.T` property will be supported for "
"2-dim arrays only. Received %d-dim array. Either "
"`arr.transpose()` or `.mT` (which swaps the last "
"two axes only) should be used instead."
"In the future, the `.T` property will be supported for "
"2-dimensional arrays only. Received %d-dimensional "
"array. Either `arr.transpose()` or `.mT` (which swaps "
"the last two axes only) should be used instead."
"(Deprecated NumPy 2.3)", ndim) < 0) {
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions 6 numpy/_core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1927,9 +1927,9 @@ gentype_transpose_get(PyObject *self, void *NPY_UNUSED(ignored))
{
/* Deprecated 2025-04-19, NumPy 2.3 */
if (DEPRECATE(
"In the future `.T` property for array scalars will "
"raise an error. If you call `.T` on an array scalar "
"intentionally you can safely drop it. In other cases "
"In the future, the `.T` property for array scalars will "
"raise an error. If you called `.T` on an array scalar "
"intentionally, you can safely drop it. In other cases, "
"`arr.transpose()` or `.mT` (which swaps the last "
"two axes only) should be used instead. "
"(Deprecated NumPy 2.3)") < 0) {
Expand Down
58 changes: 16 additions & 42 deletions 58 numpy/_core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
import numpy as np
from numpy.testing import assert_raises, temppath

try:
import pytz # noqa: F401
_has_pytz = True
except ImportError:
_has_pytz = False


class _DeprecationTestCase:
# Just as warning: warnings uses re.match, so the start of this message
Expand All @@ -43,8 +37,7 @@ def setup_method(self):
def teardown_method(self):
self.warn_ctx.__exit__()

def assert_deprecated(self, function, num=1, msg_patterns=None,
ignore_others=False,
def assert_deprecated(self, function, num=1, ignore_others=False,
function_fails=False,
exceptions=np._NoValue,
args=(), kwargs={}):
Expand All @@ -62,11 +55,6 @@ def assert_deprecated(self, function, num=1, msg_patterns=None,
The function to test
num : int
Number of DeprecationWarnings to expect. This should normally be 1.
msg_patterns : str or tuple of str
Patterns for which warning messages should match. For `str` each
warning should match to the same pattern. For a tuple of `str`
each warning should match against the corresponding pattern.
For `None` this check is skipped.
ignore_others : bool
Whether warnings of the wrong type should be ignored (note that
the message is not checked)
Expand Down Expand Up @@ -100,14 +88,6 @@ def assert_deprecated(self, function, num=1, msg_patterns=None,
# just in case, clear the registry
num_found = 0
for warning in self.log:
if msg_patterns is not None:
pattern = (msg_patterns if isinstance(msg_patterns, str) else
msg_patterns[num_found])
msg = warning.message.args[0]
if re.match(pattern, msg) is None:
raise AssertionError(
"expected %s warning message pattern but got: %s" %
(pattern, msg))
if warning.category is self.warning_cls:
num_found += 1
elif not ignore_others:
Expand Down Expand Up @@ -157,17 +137,9 @@ def test_assert_deprecated(self):
lambda: None)

def foo():
warnings.warn("foo bar", category=DeprecationWarning,
stacklevel=2)

def foo_many():
warnings.warn("foo", category=DeprecationWarning, stacklevel=2)
warnings.warn("bar", category=DeprecationWarning, stacklevel=2)

test_case_instance.assert_deprecated(foo)
test_case_instance.assert_deprecated(foo, msg_patterns="foo")
test_case_instance.assert_deprecated(foo_many, num=2,
msg_patterns=("foo", "^bar$"))
test_case_instance.teardown_method()


Expand Down Expand Up @@ -476,18 +448,20 @@ def test_deprecated(self):
)


class TestDeprecatedTNon2Dim(_DeprecationTestCase):
# Deprecated in Numpy 2.3, 2025-04
class TestDeprecatedTPropScalar(_DeprecationTestCase):
# Deprecated in Numpy 2.3, 2025-05
message = ("In the future, the `.T` property for array scalars will "
"raise an error.")

def test_deprecated(self):
self.assert_deprecated(lambda: np.int64(1).T)


class TestDeprecatedTPropNon2Dim(_DeprecationTestCase):
# Deprecated in Numpy 2.3, 2025-05
message = ("In the future, the `.T` property will be supported for "
r"2-dimensional arrays only. Received \d+-dimensional array.")

def test_deprecated(self):
self.assert_deprecated(
lambda: np.int64(1).T,
msg_patterns="In the future `.T` property for "
"array scalars will raise an error."
)
for shape in [(5,), (2, 3, 4)]:
self.assert_deprecated(
lambda: np.ones(shape).T,
msg_patterns="In the future `.T` property will be "
"supported for 2-dim arrays only. "
f"Received {len(shape)}-dim array."
)
self.assert_deprecated(lambda: np.ones(shape).T)
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.