Skip to content

Tinche/incant

Repository files navigation

incant🪄: a little magic for your functions

PyPI Build Coverage Supported Python versions Black


incant is a Python open source library for composing and calling functions. Going by the old, humorous adage that dependency injection is simply passing arguments to functions, incant is a toolkit that is well suited to that use case.

incant will help you with:

  • all kinds of dependency injection scenarios
  • adapting interfaces of third-party libraries to make them more flexible
  • generically wrapping functions and coroutines for instrumentation
  • creating signature-altering decorators
  • calling functions and coroutines with unknown signatures safely
  • ... and much more!

For example:

from incant import Incanter

incanter = Incanter()

@incanter.register_by_name
def now() -> float:
    """
    Return the current timestamp.

    We can replace this for testing later.
    """
    from time import time

    return time()

def my_function(now: float) -> None:
    print(f"The time now is {now}")

incanter.compose_and_call(my_function)

incant has a fully type-annotated interface for use with Mypy and Pyright. incant works by generating Python code at runtime, and is extremely efficient. (incant is the fastest Python dependency injection framework we're aware of.)

If you're familiar with pytest, incant dependency factories are roughly equivalent to pytest fixtures.

Project Information

License

incant is written by Tin Tvrtković and distributed under the terms of the Apache-2.0 license.