Welcome to incant🪄!#
incant is a Python open source library for composing and calling functions. Dependency injection is but one of many use-cases served well by incant; wrapping functions with context managers for observability is another.
incant includes support for:
a convenience layer with a simple API, based on a powerful and fast performance layer
a very flexible system for matching dependencies using
inspect.Parameter
, including the parameter name, type annotation and default valuesync and async functions and dependencies
sync and async context manager dependencies
no global state
attaching arbitrary external dependencies to functions (forced dependencies), commonly used for side-effects
incant has a very lean API surface, the core API being:
a single class,
incant.Incanter
, for keeping state (dependency rules)a method for registering dependencies,
Incanter.register_hook()
, and a number of higher level, more user-friendly helpers (Incanter.register_by_name()
,Incanter.register_by_type()
)methods for calling arbitrary functions while composing them with their dependencies,
Incanter.compose_and_call()
and its async variant,Incanter.acompose_and_call()
methods for invoking arbitrary functions while picking and forwarding any required arguments,
Incanter.incant()
and its async variant,Incanter.aincant()
methods for composing functions in advance, without calling them,
Incanter.compose()
methods for adjusting arbitrary functions to arbitrary parameters,
Incanter.adapt()
incant is able to leverage runtime type annotations but is also capable of functioning without them. incant is also fully type-annotated for use with Mypy and smart editors.
The Tutorial contains a walkthough and some real-life use cases of incant.
If you’re coming from a pytest background, incant dependency factories are roughly equivalent to pytest fixtures.
Installation#
To install incant, simply:
$ pip install incant