Skip to main content

Typed resolver (dependency container) for Python

Project description

tres

Typed resolver (dependency container) for Python

It provides a dependency container for you to use in typed dependency resolution.

That's all. Very type resolution. Much wow.

Inspired by the dependency container in tsyringe, but more Pythonic.

Usage:

from tres import container, InjectionToken

def a(n: int) -> str:
    return str(n)


def b(a: int, b: int) -> int:
    return a + b


a_token = InjectionToken[Callable[[int], str]]()
b_token = InjectionToken[Callable[[int, int], int]]()

container.register(a_token, a)
container.register(b_token, b)
container.register(b_token, a)  # type error


def c(f: Callable[[int], str]):
    print(f(1))


c(container[a_token])
c(container[b_token])  # type error

A longer example registering a Protocol

# application logic

from typing import Protocol, Iterable

class OrdersProtocol(Protocol):
    def byId(self, id) -> Order:
        ...

    def getLines(self, id) -> Iterable[OrderLine]:
        ...

OrdersStoreToken = tres.InjectionToken[OrdersProtocol]()

def calculate_total(orders_store: OrdersProtocol, order_id):
    order = orders_store.byId(order_id)
    lines = orders_store.getLines(order_id)
    return sum(line.price for line in lines) + order.shipping


# implementation

from config import URLfrom domain import Order, OrderLine
from application import OrdersProtocol, OrdersStoreToken

class OrdersStore(OrdersProtocol):
    def __init__(self, url):
        self.url = url

    def byId(self, id):
        return map(Order, requests.get(f'{self.url}/order/{id}').json())

    def getLines(self, id):
        return map(OrderLine, requests.get(f'{self.url}/order/{id}/lines').json())

tres.container.register(OrdersStoreToken, OrdersStore(URL))


# consumer

from application import calculate_total, OrdersStoreToken

def order_view(order_id):
    orders_store = tres.container[OrdersStoreToken]
    order = orders_store.byId(order_id)
    total = calculate_total(orders_store, order_id)
    return f'{order.id} - {order.date}: {total}'

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

tres-1.3.tar.gz (2.9 kB view hashes)

Uploaded Source

Built Distribution

tres-1.3-py2.py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 2 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