Skip to main content

Light, flexible, and pythonic dependency injection framework

Project description

Project Description

Strappy is a light, flexible, and pythonic dependency injection framework.
It combines an intuitive API with robust and convenient features:

  • No external dependencies
  • Integrates with FastAPI out of the box
  • Helpful and accurate type annotations
  • Default functionality that covers the vast majority of use cases, and easy customization for the rest

Installing

Strappy is available on PyPI.

python -m pip install strappy-di

Quick Start

Strappy makes it easy to bootstrap your application by recursively resolving dependencies for any callable.

import strappy

class Client:
    ...

class Service:
    def __init__(self, client: Client) -> None:
        self.client = client

service = strappy.base.resolve(Service)

Strappy works by using strategies to look up providers within the context of a given container. Providers fulfill your dependencies either by returning a registered instance or by calling a factory, which can itself recursivley resolve dependencies from the container's context.

Using Containers

Strappy provides a global base container strappy.base that can be used to register and resolve shared needs. This base container can also be extended to define different contexts for different use cases, such as having one for your deployed service and another for unit tests.

import strappy

deployment_container = strappy.base.extend()
test_container = strappy.base.extend()

Any changes to the base container will be reflected in its children unless explicitly overridden, but changes to child containers do not affect their parents or siblings.

Registering Providers

A Provider can fulfill a dependency either by returning an instance or by returning the result of calling a factory.

from strappy import Provider

class Foo:
    ...

# Provider with an instance
Provider(instance=Foo(...))

# Provider with a factory class
Provider(Foo)

# Provider with a factory function
Provider(lambda: Foo(...))

Providers that use factories can also be configured with arguments and a scope that allows you to cache and reuse the result.

The return type of a provider determines how it is looked up in a container's registry. This type can usually be inferred from its factory or instance, but in some cases it can be useful to set this explicitly. This value can be used by type checkers to ensure that the provider's factory or instance is of the expected type.

from typing import Protocol

class FooLike(Protocol):
    ...

provider_1 = Provider[FooLike](...)
provider_2 = Provider(..., provides=FooLike)

These providers can be added to a container and will then be available in its registry.

container.add(provider_1)

container.registry # {FooLike: [provider_1]}

Strappy also exposes a decorator syntax for registering factories.

@container.register
class Service:
    ...

@container.register
def get_service() -> Service:
    ...

Customizing Strategies

Strappy gives you full control over your container's strategies and their precedence. It comes with a number of pre-defined strategies for common use cases, such as using a FastAPI-style dependency, looking up a registered provider by type, and using a type annotation as a factory class. But it is also easy to use custom strategies by passing in any callable that accepts a parameter and a container and optionally returns a provider.

def my_custom_strategy(param: inspect.Parameter, container: ContainerLike) -> Provider | None:
    ...

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

strappy_di-0.2.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strappy_di-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file strappy_di-0.2.0.tar.gz.

File metadata

  • Download URL: strappy_di-0.2.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for strappy_di-0.2.0.tar.gz
Algorithm Hash digest
SHA256 721ed25e5b40602e2520836e575b5bb9aead92233fdca5d2ec86892ab347ea34
MD5 861d898475765eeee0fbb96ee2c5fcaa
BLAKE2b-256 3c71db8255500d5aa19c9bdffe76fc7a6e222e24ea453e9b039489adf31e8805

See more details on using hashes here.

File details

Details for the file strappy_di-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: strappy_di-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for strappy_di-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36c29a064e698228223626793256a115b9cd051616afe0d7c8851306a941c769
MD5 1b3d15315bfa74decbbd431b58d58473
BLAKE2b-256 867a25e9083479bea4944818bfcbaad27c5b044316eca429c30cc5a41655cb21

See more details on using hashes here.

Supported by

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