Draft: MAINT, ENH: Tuple-spec array_function_dispatch #31943
Open
eendebakpt wants to merge 4 commits into
Open
Draft: MAINT, ENH: Tuple-spec array_function_dispatch #31943eendebakpt wants to merge 4 commits into
array_function_dispatch #31943eendebakpt wants to merge 4 commits into
Conversation
Allow @array_function_dispatch to accept a tuple of relevant argument names instead of a callable dispatcher. Names are resolved to (kwname, position) pairs at decoration time; dispatcher_vectorcall extracts the relevant args directly in C and skips the override machinery when every one is an exact ndarray or a basic Python type. Keyword-only args are never matched positionally and positional-only args never by keyword, so signature-invalid calls still raise TypeError. Migrate ~260 trivial dispatchers to the new form (~600 lines removed). The legacy callable form is unchanged and still used by dispatchers with sequence arguments (concatenate, stack, ...); it also benefits from an early return in get_implementing_args_and_methods when all args are override-free. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
44866c2 to
cc4a740
Compare
jorenham
reviewed
Jul 12, 2026
Comment on lines
+203
to
+205
| if is_tuple_spec: | ||
| spec = _resolve_relevant_arg_spec(implementation, dispatcher) | ||
| public_api = _ArrayFunctionDispatcher(spec, implementation) |
Member
There was a problem hiding this comment.
did you verify that this didn't affect the inspect.signature of the decorated functions?
Contributor
Author
There was a problem hiding this comment.
I added a parameterized test test_inspect_signature_matches_implementation to check for this.
eea450b to
1a938d7
Compare
1a938d7 to
cbd471f
Compare
Strengthen test_inspect_sum (previously only checked that `axis` is a parameter) and verify inspect.signature equals the implementation's signature for both dispatcher forms, including positional-only and keyword-only relevant args and submodule functions. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
We allow
@array_function_dispatchto take a tuple of relevant-argument names instead of a callable dispatcher. The names are resolved to(kwname, position)pairs at decoration time. The tuple approach avoids the overhead of calling the dispatcher and creating an intermediate tuple. Fast paths are added for exact numpy arrays and python basic types.Fixes #31866.
About 260 trivial dispatchers are migrated (~600 lines removed); the callable form remains for sequence dispatchers
Benchmark results (Python 3.14):
np.sinis the control (not dispatched through@array_function_dispatch).Benchmark script
AI Disclosure
Claude Opus 4.x was used to find optimizations for the dispatcher overhead. The corresponding issue contains an alternative. After selecting this option, Claude was used to refine the PR.