Skip to content

DOC: Fix code snippet in subclassing ndarray (User Guide docs) - #32855

Open
maaz7409 wants to merge 1 commit into
numpy:mainfrom
maaz7409:fix-doc
Open

maaz7409 wants to merge 1 commit into
numpy:mainfrom
maaz7409:fix-doc

Conversation

@maaz7409

@maaz7409 maaz7409 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

PR summary

Currently, line 639 of doc/source/user/basics.subclassing.rst

super().__array_wrap__(self, out_arr, context, return_scalar)

explicitly passes self parameter.

When code is ran according to doc snippet, it raises TypeError: takes at most 3 arguments (4 given) as shown below

>>> obj = MySubClass(np.arange(5), info='spam')
In __array_finalize__:
   self is MySubClass([0, 1, 2, 3, 4])
   obj is array([0, 1, 2, 3, 4])
>>> arr2 = np.arange(5)+1
>>> ret = np.add(arr2,obj)
In __array_wrap__:
   self is MySubClass([0, 1, 2, 3, 4])
   arr is array([1, 3, 5, 7, 9])
In __array_wrap__:
   self is MySubClass([0, 1, 2, 3, 4])
   arr is array([1, 3, 5, 7, 9])
In __array_wrap__:
   self is MySubClass([0, 1, 2, 3, 4])
   arr is array([1, 3, 5, 7, 9])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/workspaces/numpy/doc/bug-reports/tests/test1.py", line 22, in __array_wrap__
    return super().__array_wrap__(self, out_arr, context, return_scalar)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: __array_wrap__() takes at most 3 arguments (4 given)

This PR fixed it by simply removing self being passed as 1st argument, because when calling parent class method super(), self shouldn't be passed explicitly.

super().__array_wrap__(out_arr, context, return_scalar)

I've tested this and it works.

>>> obj = MySubClass(np.arange(5), info='spam')
In __array_finalize__:
   self is MySubClass([0, 1, 2, 3, 4])
   obj is array([0, 1, 2, 3, 4])
>>> arr2 = np.arange(5)+1
>>> ret = np.add(arr2,obj)
In __array_wrap__:
   self is MySubClass([0, 1, 2, 3, 4])
   arr is array([1, 3, 5, 7, 9])
In __array_finalize__:
   self is MySubClass([1, 3, 5, 7, 9])
   obj is MySubClass([0, 1, 2, 3, 4])
>>> ret
MySubClass([1, 3, 5, 7, 9])
>>> ret.info
'spam'

AI Disclosure

Claude Code was used in identifying this bug.

This branch has not been deployed

No deployments
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.

1 participant