# tensorplay.overrides Source: https://www.tensorplay.cn/docs/overrides.html This module exposes various helper functions for the __tensorplay_function__ protocol. See [Extending TensorPlay](/docs/notes/extending.html#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()](#tensorplay.overrides.is_tensor_like) and [tensorplay.overrides.is_tensor_method_or_property()](#tensorplay.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()](#tensorplay.overrides.has_tensorplay_function) (and the _unary / _variadic variants) asks whether any argument in an argument tuple will intercept the call. - [tensorplay.overrides.handle_tensorplay_function()](#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()](#tensorplay.overrides.get_ignored_functions) and [tensorplay.overrides.get_overridable_functions()](#tensorplay.overrides.get_overridable_functions) enumerate the dispatchable surface — the functions that do and do not route through the protocol. - [tensorplay.overrides.get_testing_overrides()](#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()](#tensorplay.overrides.wrap_tensorplay_function) wraps a public API so custom classes are routed through your wrapper first. - [tensorplay.overrides.resolve_name()](#tensorplay.overrides.resolve_name) renders a function reference as its fully qualified name, and [tensorplay.overrides.redispatch_function()](#tensorplay.overrides.redispatch_function) re-invokes the original implementation for arguments that did not actually need interception. - [tensorplay.overrides.TensorPlayFunctionMode](/docs/generated/tensorplay.overrides.TensorPlayFunctionMode.html#tensorplay.overrides.TensorPlayFunctionMode) is 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 ```python tensorplay.overrides.get_ignored_functions() → set[Callable[[...], Any]] ``` Return public callables that do not receive function-hook dispatch. ```python tensorplay.overrides.get_overridable_functions() → dict[Any, list[Callable[[...], Any]]] ``` Return namespaces and callables that participate in function hooks. ```python tensorplay.overrides.resolve_name(f: Any) → str | None ``` Return the stable public name registered for a callable. ```python tensorplay.overrides.get_testing_overrides() → dict[Callable[[...], Any], Callable[[...], int]] ``` Return signature-preserving sentinels for every overridable callable. ```python tensorplay.overrides.handle_tensorplay_function(public_api: Callable[[...], _R], relevant_args: Iterable[Any], *args: Any, **kwargs: Any) → _R ``` 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 NotImplemented to pass control to the next candidate. ```python tensorplay.overrides.has_tensorplay_function(relevant_args: Iterable[Any]) → bool ``` Return whether any argument supplies a Python dispatch hook. ```python tensorplay.overrides.is_tensor_like(inp: Any) → bool ``` Return whether inp is a Tensor (any subclass included) or implements a dispatch hook. ```python tensorplay.overrides.is_tensor_method_or_property(func: Callable[[...], Any]) → bool ``` Return whether func is a Tensor method or property getter. ```python tensorplay.overrides.wrap_tensorplay_function(dispatcher: Callable[[...], Iterable[Any]]) → Callable[[Callable[[...], _R]], Callable[[...], _R]] ``` Decorate a composite function with hook discovery and dispatch. ```python tensorplay.overrides.redispatch_function(func: Any, types_: Iterable[type], args: Iterable[Any] | None, kwargs: dict[str, Any] | None) → _R ``` Skip the current native dispatch layer and invoke func again. ## Modes | TensorPlayFunctionMode |Context manager that handles function hooks for a dynamic scope. | | --- | --- |