latest (dev)
Copy
Latest development documentation · Updated 2026-10-08
tensorplay.overrides
This module exposes various helper functions for the __tensorplay_function__
protocol. See Extending TensorPlay for more details on the
__tensorplay_function__ protocol.
The protocol is how non-tensor objects participate in tensor operations: when any
argument of a tensorplay operation implements __tensorplay_function__, the operation
delegates to that method instead of running its default path. The functions here are the
plumbing both sides use — the predicates that detect such arguments, the dispatcher that
hands control to them, and the utilities for testing and wrapping.
tensorplay.overrides.is_tensor_like()andtensorplay.overrides.is_tensor_method_or_property()classify an object from the tensor side: does it look like a tensor, and is a given name part of the tensor API?tensorplay.overrides.has_tensorplay_function()(and the_unary/_variadicvariants) asks whether any argument in an argument tuple will intercept the call.tensorplay.overrides.handle_tensorplay_function()performs the delegation: given the public API callable, the relevant args, and the full call arguments, it invokes the highest-priority__tensorplay_function__implementation.tensorplay.overrides.get_ignored_functions()andtensorplay.overrides.get_overridable_functions()enumerate the dispatchable surface — the functions that do and do not route through the protocol.tensorplay.overrides.get_testing_overrides()builds a table of fake overrides for every overridable function, which is how the protocol itself is tested.tensorplay.overrides.wrap_tensorplay_function()wraps a public API so custom classes are routed through your wrapper first.tensorplay.overrides.resolve_name()renders a function reference as its fully qualified name, andtensorplay.overrides.redispatch_function()re-invokes the original implementation for arguments that did not actually need interception.tensorplay.overrides.TensorPlayFunctionModeis the mode-based form of the protocol: a context object whose__tensorplay_function__sees every dispatchable call made inside its scope, without any argument implementing the protocol.
The gate and the delegation, as an operation’s public entry point uses them:
import tensorplay as tp
from tensorplay.overrides import has_tensorplay_function, handle_tensorplay_function
m = tp.ones(2)
assert not has_tensorplay_function((m,)) # plain tensors do not intercept
# inside a public API, the two calls pair up exactly like this:
if has_tensorplay_function((m, m)): # False here — the default path runs
result = handle_tensorplay_function(tp.add, (m, m), m, m)
else:
result = tp.add(m, m)
handle_tensorplay_function is only meaningful behind the gate: the second argument is
the tuple inspected for interceptors, everything after it is the actual call, and with
no interceptor present it raises TypeError — “no implementation found” — rather than
falling back. Operations expose this pairing so one custom argument reroutes the whole
call to your implementation.
Functions
- tensorplay.overrides.get_ignored_functions() set[Callable[[...], Any]][source]
Return public callables that do not receive function-hook dispatch.
- tensorplay.overrides.get_overridable_functions() dict[Any, list[Callable[[...], Any]]][source]
Return namespaces and callables that participate in function hooks.
- tensorplay.overrides.resolve_name(f: Any) str | None[source]
Return the stable public name registered for a callable.
- tensorplay.overrides.get_testing_overrides() dict[Callable[[...], Any], Callable[[...], int]][source]
Return signature-preserving sentinels for every overridable callable.
- tensorplay.overrides.handle_tensorplay_function(public_api: Callable[[...], _R], relevant_args: Iterable[Any], *args: Any, **kwargs: Any) _R[source]
Dispatch a composite API through function hooks and active modes.
Distinct operand types are ordered from the most-derived type to the least-derived type; unrelated types retain their first-seen order. A hook may return
NotImplementedto pass control to the next candidate.
- tensorplay.overrides.has_tensorplay_function(relevant_args: Iterable[Any]) bool[source]
Return whether any argument supplies a Python dispatch hook.
- tensorplay.overrides.is_tensor_like(inp: Any) bool[source]
Return whether
inpis a Tensor (any subclass included) or implements a dispatch hook.
- tensorplay.overrides.is_tensor_method_or_property(func: Callable[[...], Any]) bool[source]
Return whether
funcis a Tensor method or property getter.
Modes
Context manager that handles function hooks for a dynamic scope. |
Help improve this page
Found an error, an unclear step, or a missing example?

