baybe.utils.basic.register_hooks

baybe.utils.basic.register_hooks(target: Callable, pre_hooks: Sequence[Callable] | None = None, post_hooks: Sequence[Callable] | None = None)[source]

Register custom hooks with a given target callable.

The provided hooks need to be “compatible” with the target in the sense that their signatures can be aligned:

  • The hook signature may only contain parameters that also exist in the target callable (<– basic requirement).

  • However, parameters that are not needed by the hook can be omitted from its signature. This requires that the parameters of both signatures can be matched via their names. For simplicity, it is thus assumed that the hook has no positional-only arguments.

  • If an annotation is provided for a hook parameter, it must match its target counterpart (<– safety mechanism to prevent unintended argument use). An exception is when the target parameter has no annotation, in which case the hook annotation is unrestricted. This is particularly useful when registering hooks with methods, since it offers the possibility to annotate the “self” parameter bound to the method-carrying object, which is typically not annotated in the target callable.

Parameters:
Return type:

Callable

Returns:

The wrapped callable with the hooks attached.

Raises:
  • TypeError – If any hook has positional-only arguments.

  • TypeError – If any hook expects parameters that are not present in the target.

  • TypeError – If any hook has a non-empty parameter annotation that does not match with the corresponding annotation of the target.