Skip to content

NumPy 2.6: Fix __array_wrap__ method name detection - #12720

Merged
crusaderky merged 7 commits into
dask:mainfrom
crusaderky:fix/array-wrap-stack-inspection
Nov 16, 2026
Merged

crusaderky merged 7 commits into
dask:mainfrom
crusaderky:fix/array-wrap-stack-inspection

Conversation

@crusaderky

@crusaderky crusaderky commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

dask/dataframe/tests/test_ufunc.py::test_ufunc_wrapped_not_implemented fails on nightly
NumPy (2.6.0.dev0):

AssertionError: Regex pattern did not match.
Expected regex: '`repeat` is not implemented'
Actual message: '`pytest_pyfunc_call` is not implemented for `dask.dataframe.Series`.'

Cause

DataFrame.__array_wrap__ and Series.__array_wrap__ in dask/dataframe/dask_expr/_collection.py derived the name of the calling NumPy function with a hardcoded stack index, inspect.stack()[3][3]. NumPy 2.6 changed the call path of np.repeat, so frame 3 is now pytest's pytest_pyfunc_call rather 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.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Unit Test Results

See 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
 19 932 tests + 2   18 503 ✅ + 2   1 429 💤 ±0  0 ❌ ±0 
436 330 runs  +44  384 861 ✅ +50  51 469 💤  - 6  0 ❌ ±0 

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
crusaderky force-pushed the fix/array-wrap-stack-inspection branch from 3497a66 to 2dceb2d Compare November 13, 2026 14:03
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.
@crusaderky crusaderky changed the title [unsupervised AI] Fix __array_wrap__ method name detection NumPy 2.6: Fix __array_wrap__ method name detection Nov 13, 2026
@crusaderky
crusaderky marked this pull request as ready for review November 13, 2026 18:06
@crusaderky
crusaderky merged commit 4dd05f3 into dask:main Nov 16, 2026
38 of 40 checks passed
@crusaderky
crusaderky deleted the fix/array-wrap-stack-inspection branch November 16, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant