pytorch - ✅(Solved) Fix BUG: Avoid NumPy 2 deprecation warning in `__array_wrap__` [2 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#180657Fetched 2026-04-18 05:51:34
View on GitHub
Comments
0
Participants
1
Timeline
23
Reactions
0
Author
Participants
Timeline (top)
mentioned ×8subscribed ×8labeled ×6cross-referenced ×1

Error Message

warnings.filterwarnings("error") # with this an error

Root Cause

I'll guess end-users use this path but because of how DeprecationWarnings work they won't actually see them normally, so it makes sense it wasn't spotted. It would be nice to escalate the warning to something visible, but I would prefer if by that time torch has the fix.

Fix Action

Fixed

PR fix notes

PR #31194: DEP: make deprecation visible for __array_wrap__

Description (problem / solution / changelog)

PR summary

<!-- Please take some time to make it easier for us maintainers to understand and review your PR. Describe the pull request, using the questions below as guidance, and link to any relevant issues and PRs. Also, have you hit all [the guidelines](https://numpy.org/devdocs/dev/index.html#guidelines)? And have you filled out the disclosure section below? -->

This PR convert current DeprecationWarning to VisibleDeprecationWarning for numpy.ndarray.__array_wrap__. This PR was made related to this comment.

AI Disclosure

<!-- If AI was used in the preparation of this pull request, please disclose the tool(s) used, how they were used, and specify what code or text is AI generated. If no AI tools were used, please write "No AI tools used" in this section. Read our policy on AI generated code at https://numpy.org/devdocs/dev/ai_policy.html. In particular, all interaction is to be done by humans, including submission of PRs. -->

No AI tools used.

Changed files

  • doc/release/upcoming_changes/31194.deprecation.rst (added, +2/-0)
  • numpy/_core/src/multiarray/arraywrap.c (modified, +1/-1)
  • numpy/_core/tests/test_deprecations.py (modified, +1/-1)

Code Example

import warnings

import numpy as np
import torch

warnings.filterwarnings("error")  # with this an error

np.delete(torch.arange(10), 0)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

In NumPy 2, NumPy added a DeprecationWarning on __array_wrap__ to extend the information passed in and avoid an indefinite try/except chain.

This means that the e.g. the following gives a warning:

import warnings

import numpy as np
import torch

warnings.filterwarnings("error")  # with this an error

np.delete(torch.arange(10), 0)

Now, the solution is straight forward: Just add the two new arguments and ignore them. (torch has no scalars, and the context is info that was always there and you don't need). That is __array_wrap__(obj, context=None, return_scalar=None).

I'll guess end-users use this path but because of how DeprecationWarnings work they won't actually see them normally, so it makes sense it wasn't spotted. It would be nice to escalate the warning to something visible, but I would prefer if by that time torch has the fix.

Versions

current main.

cc @mruberry @rgommers

extent analysis

TL;DR

Update the __array_wrap__ method in torch to include the new arguments context and return_scalar to avoid the DeprecationWarning.

Guidance

  • Identify the __array_wrap__ method in torch and update its signature to __array_wrap__(obj, context=None, return_scalar=None).
  • Ignore the context and return_scalar arguments as they are not relevant to torch.
  • Test the updated method to ensure it resolves the DeprecationWarning issue.
  • Consider escalating the warning to a visible error to ensure it is addressed before the torch fix is released.

Example

class TorchArray:
    def __array_wrap__(self, obj, context=None, return_scalar=None):
        # Ignore context and return_scalar, and handle obj as before
        pass

Notes

This fix assumes that the __array_wrap__ method is the source of the DeprecationWarning and that updating its signature will resolve the issue.

Recommendation

Apply workaround: Update the __array_wrap__ method to include the new arguments, as this will resolve the DeprecationWarning issue and allow torch to continue functioning as expected until a permanent fix is released.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix BUG: Avoid NumPy 2 deprecation warning in `__array_wrap__` [2 pull requests, 1 participants]