Skip to content

Conversation

@jorenham
Copy link
Member

Closes #30283

@jorenham jorenham added component: numpy.lib 07 - Deprecation 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes labels Nov 23, 2025
@jorenham jorenham force-pushed the deprecate-lib.user_array.container branch from c89abe3 to 7a06475 Compare November 23, 2025 13:48
@jorenham jorenham removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Nov 23, 2025
@jorenham jorenham added this to the 2.4.0 release milestone Nov 23, 2025
astype
"""
def __init_subclass__(cls) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Does this work? AFAICT, it will only be called when a subclass is instantiated.

Copy link
Member Author

@jorenham jorenham Nov 24, 2025

Choose a reason for hiding this comment

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

First thing I tried was warning on import, but distutils didn't like that for some reason, so I went with this approach.
If you prefer, I could try the module-level __getattr__ trick?

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 you can test with:

python numpy/lib/_user_array_impl.py

There is also an existing error in that test :)

Why not add the deprecation to the __init__ ?

Copy link
Member Author

@jorenham jorenham Nov 24, 2025

Choose a reason for hiding this comment

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

I think you can test with:

python numpy/lib/_user_array_impl.py

I kinda assumed that the only use-case of contasiner was as a base class. I doubt that anyone would use it directly like done in this test. But maybe I'm wrong 🤷

There is also an existing error in that test :)

That's pretty funny haha

Why not add the deprecation to the __init__ ?

There's a higher chance of someone overriding that I guess. But I maybe I could do both; what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Let's ping @stefanv, he is one of the early editors of the file.

@charris
Copy link
Member

charris commented Nov 25, 2025

This class is actually documented in arrays.classes.rst, and it comes from numeric UserArray. It seems the idea was to allow subclassing of the numeric arrays implemented in C, the original masked arrays would be an example of such use. Grok offers an example:

from Numeric import array, Float
from UserArray import UserArray

class MyLabeledArray(UserArray):
    def __init__(self, data, label=""):
        UserArray.__init__(self, array(data, Float))
        self.label = label

    def __repr__(self):
        return f"<MyLabeledArray {self.label}: {UserArray.__repr__(self)}>"'

and offers units as another application. I suspect the motivation was difficulty inheriting from C classes.

In any case, you are correct that the resulting Python class was intended as a base class. I'm not sure it wasn't a better idea than the current masked array implementation :) Have you tested __init_subclass__?

@jorenham
Copy link
Member Author

jorenham commented Nov 25, 2025

This class is actually documented in arrays.classes.rst, and it comes from numeric UserArray. It seems the idea was to allow subclassing of the numeric arrays implemented in C, the original masked arrays would be an example of such use. Grok offers an example:

from Numeric import array, Float
from UserArray import UserArray

class MyLabeledArray(UserArray):
    def __init__(self, data, label=""):
        UserArray.__init__(self, array(data, Float))
        self.label = label

    def __repr__(self):
        return f"<MyLabeledArray {self.label}: {UserArray.__repr__(self)}>"'

and offers units as another application. I suspect the motivation was difficulty inheriting from C classes.

Ohh, that goes pretty far back then!

Have you tested __init_subclass__?

Yup:

from numpy.lib.user_array import container

class MyArray(container):
    pass

Running this demo.py with spin python demo.py (python 3.11) shows:

/home/joren/Workspace/numpy/demo.py:3: DeprecationWarning: The numpy.lib.user_array.container class is deprecated and will be removed in a future version.
  class MyArray(container):

@charris
Copy link
Member

charris commented Nov 25, 2025

Sounds good them. Should probably leave a note in the documentation as well, search for UserArray.

@charris
Copy link
Member

charris commented Nov 25, 2025

Oh, and please add a comment with the date and release, something like:

# Deprecated in NumPy 2.4, 2025-11-24

@jorenham
Copy link
Member Author

Oh, and please add a comment with the date and release, something like:

# Deprecated in NumPy 2.4, 2025-11-24

Sharp; I completely forgot about that

@charris charris merged commit 2c2a0d3 into numpy:main Nov 25, 2025
76 checks passed
@charris
Copy link
Member

charris commented Nov 25, 2025

Thanks Joren.

@jorenham jorenham deleted the deprecate-lib.user_array.container branch November 25, 2025 22:12
cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
* DEP: deprecate ``numpy.lib.user_array.container``

* DEP: add deprecation notice for ``numpy.lib.user_array.container``

* DEP: add deprecation comment with date to ``lib.user_array.container``

Co-authored-by: Charles Harris <[email protected]>

---------

Co-authored-by: Charles Harris <[email protected]>
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
* DEP: deprecate ``numpy.lib.user_array.container``

* DEP: add deprecation notice for ``numpy.lib.user_array.container``

* DEP: add deprecation comment with date to ``lib.user_array.container``

Co-authored-by: Charles Harris <[email protected]>

---------

Co-authored-by: Charles Harris <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DEP: Deprecating lib.user_array.container

2 participants