Repository navigation
NumPy 2.6: Fix __array_wrap__ method name detection - #12720
Merged
crusaderky merged 7 commits intoNov 16, 2026
Merged
Conversation
Contributor
Unit Test ResultsSee test report for an extended history of previous test failures. This is useful for diagnosing flaky tests. 29 files ± 0 29 suites ±0 6h 26m 41s ⏱️ - 38m 59s Results for commit 897f6fe. ± Comparison against base commit 249a51a. ♻️ This comment has been updated with latest results. |
Make __array_wrap__ method name detection robust across NumPy versions The previous sys._getframe(1) approach named NumPy's private _wrapit helper on NumPy 2.5 (py314 CI). Walk the stack and return the first non-private frame, which is the public NumPy function (e.g. repeat) on both 2.5 and 2.6. Annotate frame variable for mypy
crusaderky
force-pushed
the
fix/array-wrap-stack-inspection
branch
from
November 13, 2026 14:03
3497a66 to
2dceb2d
Compare
The caller lookup started at a fixed ``sys._getframe(2)`` depth, which raises ValueError instead of returning when ``__array_wrap__`` has no Python caller; the previous ``inspect.stack()`` code caught IndexError for that case. The "This method" fallback was also interpolated into backticks, turning the message into "`This method` is not implemented". Walk ``f_back`` from ``sys._getframe(1)`` and return None when no non-private frame is left, so both ``__array_wrap__`` implementations keep the original unformatted fallback and the lookup cannot raise.
The reported NumPy function name depends on how many private helpers sit between the caller and ``__array_wrap__``, which changed between NumPy 2.5 and 2.6. ``test_ufunc_wrapped_not_implemented`` only exercises the chain of the installed NumPy, so a regression that reports a private helper again would be caught only on the NumPy version where it surfaced. Simulate a ``repeat`` -> ``_wrapfunc`` -> ``_wrapit`` dispatch chain to pin the contract for both DataFrame and Series independently of NumPy internals.
The caller lookup skipped private frames and returned the first public one from any module. When no NumPy function sits on the stack this named an unrelated function, which is the symptom this PR set out to fix (`pytest_pyfunc_call` is not implemented), and it left the 'This method' fallback unreachable in practice. Stop the walk at the first non-NumPy frame so the message either names a public NumPy function or falls back to 'This method'. Replace the test that mocked NumPy's helper chain with one for the fallback; the real np.repeat test already covers the helper chain across the NumPy versions in CI. Trim the duplicated docstring and comment.
__array_wrap__ method name detection
crusaderky
marked this pull request as ready for review
November 13, 2026 18:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
dask/dataframe/tests/test_ufunc.py::test_ufunc_wrapped_not_implementedfails on nightlyNumPy (2.6.0.dev0):
Cause
DataFrame.__array_wrap__andSeries.__array_wrap__indask/dataframe/dask_expr/_collection.pyderived the name of the calling NumPy function with a hardcoded stack index,inspect.stack()[3][3]. NumPy 2.6 changed the call path ofnp.repeat, so frame 3 is now pytest'spytest_pyfunc_callrather than the NumPy function that triggered the error path. The message is cosmetic but the test asserts on it, and any change in NumPy's internal call depth breaks it again.