Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MAINT: 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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Apr 10, 2025

As the title says, this PR deprecates .T property for non-2dim arrays and scalars, in order to be compatible with the Array API: link.

@mtsokol mtsokol added 03 - Maintenance 40 - array API standard PRs and issues related to support for the array API standard labels Apr 10, 2025
@mtsokol mtsokol added this to the 2.2.5 release milestone Apr 10, 2025
@mtsokol mtsokol self-assigned this Apr 10, 2025
@charris charris modified the milestones: 2.2.5 release, 2.3.0 release Apr 10, 2025
@mtsokol mtsokol force-pushed the T-non-2dim-depr branch 2 times, most recently from 621e40a to 0163784 Compare April 10, 2025 13:15
@mattip
Copy link
Member

mattip commented Apr 10, 2025

I agree with @charris’s relabeling: this should not target a point release rather 2.3

@mtsokol
Copy link
Member Author

mtsokol commented Apr 10, 2025

I agree with @charris’s relabeling: this should not target a point release rather 2.3

That's right - done!

@mtsokol
Copy link
Member Author

mtsokol commented Apr 10, 2025

To get docs build green we need matplotlib/matplotlib#29896 in first (and matplotlib bugfix release)

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Given the number of places the tests needed to be updated, I bet this will be a noisy deprecation. Maybe worth pinging the mailing list?

if (ndim != 2) {
if (PyErr_WarnFormat(PyExc_UserWarning, 1,
"In the future `.T` property will be supported for "
"2-dim arrays only. Here it is %d-dim array.",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"2-dim arrays only. Here it is %d-dim array.",
"2-dim arrays only. Received %d-dim array.",

This should also suggest how to fix code triggering the warning

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@@ -0,0 +1,2 @@
* ``arr.T`` property has been deprecated for array scalars and arrays with
dimensionality different than ``2``.
Copy link
Member

Choose a reason for hiding this comment

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

This should also suggest how to fix the deprecation warning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

"In the future `.T` property will be supported for "
"2-dim arrays only. Received %d-dim array. Either "
"`np.permute_dims(arr, range(arr.ndim)[::-1])` "
"(compatible with the Array API) or `arr.transpose()` "
Copy link
Member

Choose a reason for hiding this comment

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

I would delete the note and at least re-order this. It nudges users to something less convenient for what seems very little reason to me.
A user who needs array API is a library author and will already not use .T here anyway.

It may be good to note that .mT exists to swap the last two axes only (If just to increase awareness).

Copy link
Member Author

Choose a reason for hiding this comment

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

And what if someone uses .T to reverse axes for ndim>2 cases anyway? I can remove Array API suggestion but how about keeping arr.transpose() for people who want to retain existing behavior.

Copy link
Member

Choose a reason for hiding this comment

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

I think that's what Sebastian is saying - to leave arr.transpose() or at least mention it first and mention .mT as well.

Copy link
Member

Choose a reason for hiding this comment

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

I feel it currently makes it seem that arr.transpose() is somehow a not the preferred solution, but for those users who run into it it will almost certainly be the preferred one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for an explanation! I updated the message as you suggested.


def test_deprecated_T_non_2dim():
# Deprecated in Numpy 2.3, 2025-04
with pytest.warns(UserWarning, match="In the future `.T` property for "
Copy link
Member

Choose a reason for hiding this comment

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

Please use assert_deprecated as all other tests in this file. That ensures that the error path is also tested, even if that is rather trivial here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't use assert_deprecated because it's missing match= parameter to actually match the warning message, but I can use it instead.

Copy link
Member

Choose a reason for hiding this comment

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

because it's missing match= parameter to actually match the warning message

Seems reasonable to add it, assuming that's easy.

Copy link
Member

Choose a reason for hiding this comment

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

IIRC it does, but takes the message from the class, but not sure. And yeah, I don't actually care about using it but I do care abot a habit of testing the error path.

Copy link
Member Author

Choose a reason for hiding this comment

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

because it's missing match= parameter to actually match the warning message

Seems reasonable to add it, assuming that's easy.

I like this idea - added msg_patterns argument to assert_deprecated.

@@ -848,6 +848,18 @@ array_flat_set(PyArrayObject *self, PyObject *val, void *NPY_UNUSED(ignored))
static PyObject *
array_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
{
int ndim = PyArray_NDIM(self);
if (ndim != 2) {
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment /* DEPRECATED ... (you can find similar ones elsewhere).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

"2-dim arrays only. Received %d-dim array. Either "
"`np.permute_dims(arr, range(arr.ndim)[::-1])` "
"(compatible with the Array API) or `arr.transpose()` "
"should be used instead.",
Copy link
Member

Choose a reason for hiding this comment

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

Add (Deprecated NumPy 2....) as all our deprecation warnings should have.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@@ -848,6 +848,18 @@ array_flat_set(PyArrayObject *self, PyObject *val, void *NPY_UNUSED(ignored))
static PyObject *
array_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
{
int ndim = PyArray_NDIM(self);
if (ndim != 2) {
if (PyErr_WarnFormat(PyExc_UserWarning, 1,
Copy link
Member

Choose a reason for hiding this comment

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

Why a UserWarning? Please use a DeprecationWarning, or actually, use the DEPRECATE macro.

We could use a visible deprecation warning if we want to inform new users who accidentally get it wrong. But only if we take the trouble to check that very few libraries use this (i.e. there are few places in existing code where DeprecationWarning would be hidden, but VisibleDeprecationWarning wouldn't be).

Copy link
Member

Choose a reason for hiding this comment

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

Given the number of places the tests needed to be updated, I bet this will be a noisy deprecation.

So, it must clearly be a DeprecationWarning. This should be taken very slowly, i.e. in a year or so a VisibleDeprecationWarning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done - for array scalars in scalartypes.c.src I used DEPRECATE macro, but here I kept PyErr_WarnFormat, now with a DeprecationWarning, because it allows formatting contrary to DEPRECATE.

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
with the Array API) or can be used.
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: dangling "or" at the end here

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

@mtsokol mtsokol force-pushed the T-non-2dim-depr branch from 85cae50 to 4b970ff Compare May 10, 2025 09:00
@mtsokol
Copy link
Member Author

mtsokol commented May 10, 2025

@seberg Yesterday new matplotlib version got released. Docs build is passing so this PR can go in as well now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
03 - Maintenance 40 - array API standard PRs and issues related to support for the array API standard
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants