Skip to main content

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 URL
from 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}'

Metadata

Release files for tres 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tres 1.3
File Size Uploaded
tres-1.3.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tres 1.3
File Interpreter ABI Platform
tres-1.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 7.5 kB

Release files / tres-1.3.tar.gz

Download URL tres-1.3.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5cda155d179bd806e48d00cc0592488d8d0ce8dbce0c6c14ee7d2a6fe6fa1973
BLAKE2b-256 checksum
How to use checksums
ea506dc8b2a0ed6f50146d534ce6ec5a4768ae982205489059f6bcc44d1a638b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.7.5 Darwin/18.7.0

Release files / tres-1.3-py2.py3-none-any.whl

Download URL tres-1.3-py2.py3-none-any.whl
Size 4.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
da983bf30dcbfb6ffbfbf7d1e0ecb7a56be5252c769aa7050651bd359825d96e
BLAKE2b-256 checksum
How to use checksums
353b2b73eac3ff41972b1fcfaf371777fbc8ce125c5613e7216aabc0bbaba148
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.7.5 Darwin/18.7.0

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page