incant package#
Module contents#
- class incant.Hook(predicate: Callable[[Parameter], bool], factory: Tuple[Callable[[Parameter], Callable], Literal['sync', 'async'] | None] | None)#
Bases:
object
- factory: Tuple[Callable[[Parameter], Callable], Literal['sync', 'async'] | None] | None#
- classmethod for_type(type: Any, hook: Callable | None) Hook #
Register by exact type (subclasses won’t match).
- predicate: Callable[[Parameter], bool]#
- exception incant.IncantError#
Bases:
Exception
An Incant error.
- class incant.Incanter(hook_factory_registry: List[Hook] = _Nothing.NOTHING)#
Bases:
object
A registry of _hooks_, used for function composition.
Hooks use predicate functions to define rules of how they bind to function arguments.
- async acompose_and_call(fn: Callable[[...], Awaitable[R]], *args, **kwargs) R #
Compose fn as async and call it with the given parameters.
- adapt(fn: Callable[[...], R], *args: Callable[[Parameter], bool], **kwargs: Callable[[Parameter], bool]) Callable[[...], R] #
Adapt fn for incantation.
Args and kwargs shape the signature of the produced function.
- async aincant(fn: Callable[[...], Awaitable[R]], *args, **kwargs) R #
Invoke async fn the best way we can.
- async ainvoke(fn: Callable[[...], Awaitable[R]], *args, **kwargs) R #
Compose fn as async and call it with the given parameters.
- compose(fn: Callable[[...], R], hooks: Sequence[Hook] = (), is_async: bool | None = None, forced_deps: Sequence[Callable | Tuple[Callable, Literal['sync', 'async']]] = ()) Callable[[...], R] #
Compose fn with its satisfied dependencies, potentially creating a new function.
- Parameters:
forced_deps – A sequence of dependencies that will be used even if fn doesn’t require them explicitly.
- compose_and_call(fn: Callable[[...], R], *args, **kwargs) R #
Compose fn and call it with the given parameters.
- incant(fn: Callable[[...], R], *args, **kwargs) R #
Invoke fn the best way we can.
- invoke(fn: Callable[[...], R], *args, **kwargs) R #
Compose fn and call it with the given parameters.
- register_by_name(fn: Callable | None = None, *, name: str | None = None, is_ctx_manager: Literal['sync', 'async'] | None = None)#
Register a factory to be injected by name. Can also be used as a decorator.
If the name is not provided, the name of the factory will be used.
- register_by_type(fn: Callable | Type, type: Type | None = None, is_ctx_manager: Literal['sync', 'async'] | None = None)#
Register a factory to be injected by type. Can also be used as a decorator.
If the type is not provided, the return annotation from the factory will be used.
- register_hook(predicate: Callable[[Parameter], bool], factory: Callable, is_ctx_manager: Literal['sync', 'async'] | None = None) None #
Register a hook to be used for function composition.
- Parameters:
predicate – A predicate function, should return True if this hook should be used to fulfill a function parameter.
factory – The function that will be called to fulfil a function parameter.
is_ctx_manager – Is the factory a context manager?
- register_hook_factory(predicate: Callable[[Parameter], bool], hook_factory: Callable[[Parameter], Callable], is_ctx_manager: Literal['sync', 'async'] | None = None) None #