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.
Inspired by libraries like Punq, Strappy combines an intuitive API with robust and convenient features and accurate type annotations.

Quick Start

Strappy makes it easy to bootstrap your application by allowing you to recursively resolve dependencies for any callable, usually a class or function.

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 more narrow contexts for different use cases, such as 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:
    ...

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.1.0.tar.gz (9.6 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.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: strappy_di-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 e7cb39e6d8df7dc85017350e81b03f9170fd1723c4fb2ac126e650b6c099cb86
MD5 a2511e100302365f00d96b3cc3643f9e
BLAKE2b-256 a28e51b8f7ad58176a26083fbf84fa9d3a77b58eb952c294cefa27f433b19167

See more details on using hashes here.

File details

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

File metadata

  • Download URL: strappy_di-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8edba80152a595383b4e44cbada630ca49a85e5dc88b828d1a13531099099940
MD5 180fca31a2be113c4b7e8b2dd31369d3
BLAKE2b-256 13b841c47bda89fee70745859b4432f0d9dba53203947855b79fee9ff5337660

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