An opinionated dependency injection library for Python
Project description
PySyringe
An opinionated dependency injection library for Python.
A container that does not rely on adding decorators to your domain classes. It only wraps views in the infrastructure layer to keep your domain and app layer decoupled from the framework and the container.
Installation
pip install pysyringe
Usage
# container.py
from myapp.domain import CalendarInterface, EmailSenderInterface
from myapp.infra import LoggingEmailSender, SmtpEmailSender, Calendar
from django.core.http import HttpRequest, HttpResponse
class Factory:
"""
The factory is used to instruct how to create complex objects or to
customize dependencies based on the environment.
Any class with methods annotated with return types can be used to
resolve dependencies. There is not a concrete interface you need to implement,
the container will introspect the return types of the methods to know how to
resolve the dependencies.
"""
def __init__(self, environment: str) -> None:
self.environment = environment
def get_mailer(self) -> EmailSenderInterface:
"""
The name of the method is irrelevant, the container knows this method
is used to resolve the EmailSenderInterface because of the return type
annotation.
"""
if self.environment == "production":
return SmtpEmailSender("mta.example.org", 25)
return LoggingEmailSender()
# Create your custom factory
factory = Factory(getenv('ENVIRONMENT'))
# Initialize the container with your factory
container = Container(factory)
# You can blacklist classes yo don't want the container to try to instantiate by inference
container.never_provide(HttpRequest)
container.never_provide(HttpResponse)
# You can also alias interfaces to concrete classes
container.alias(CalendarInterface, Calendar)
# views.py
from container import container
@container.inject
def my_view(request: HttpRequest, calendar: CalendarInterface) -> HttpResponse:
now = calendar.now()
return HttpResponse(f"Hello, World! The current time is {now}")
Testing
The container can be patched for testing.
# test_service.py
from container import container
from myapp.domain import UserRepository
from myapp.usecases import SignupUserService
from myapp.infra.testing import InMemoryUserRepository
import pytest
@pytest.fixture(autouse=True)
def clear_container_mocks_after_each_test():
yield
container.clear_mocks()
def test_create_user():
user_repository = InMemoryUserRepository()
container.use_mock(UserRepository, user_repository)
signup_user_service = container.provide(SignupUserService)
signup_user_service.signup("John Doe", "john.doe@example.org")
assert user_repository.get_by_email("john.doe@example.org")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysyringe-1.0.1.tar.gz.
File metadata
- Download URL: pysyringe-1.0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Linux/6.13.4-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9efcf5bac1d9fea654af617fd2921a08ff3202c5486464bb0437c35f1cc18ea7
|
|
| MD5 |
0a67c068c49d267961b71b7dd29a8d96
|
|
| BLAKE2b-256 |
fb542ed63c91f33233409c3770593b3183949872b7f10d39920a4f5dadf019d6
|
File details
Details for the file pysyringe-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pysyringe-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Linux/6.13.4-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17e5452050afd24851ab6978b7fafba68eab22b479d7052c3c2f0bc5dde43349
|
|
| MD5 |
d4cc0f04df837ccbe6f1fd256b964910
|
|
| BLAKE2b-256 |
e618691919f4745685eb2bdaf05b39801a82b6d9f670eb9a169405a9944ca82d
|