Skip to main content

Magical function composition

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

incant-23.2.0.tar.gz (1.2 MB view hashes)

Uploaded Source

Built Distribution

incant-23.2.0-py3-none-any.whl (17.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page