Fast and easy dependency injection framework.
Project description
Basic usage
Register an injectable
Note: If the class needs dependencies, these will be resolved when the instance is retrieved.
If you wish to inject a singleton, use singleton
decorator.
from injection import singleton
@singleton
class ServiceA:
""" class implementation """
If you wish to inject a new instance each time, use injectable
decorator.
from injection import injectable
@injectable
class ServiceB:
""" class implementation """
If you have a constant (such as a global variable) and wish to register it as an injectable, use set_constant
function.
from injection import set_constant
class ServiceC:
""" class implementation """
service_c = ServiceC()
set_constant(service_c)
Or here is the decorator constant
which is equivalent:
Unlike
@singleton
, dependencies will not be resolved.
from injection import constant
@constant
class ServiceC:
""" class implementation """
Inject an instance
To inject one or several instances, use inject
decorator.
Don't forget to annotate type of parameter to inject.
from injection import inject
@inject
def some_function(service_a: ServiceA):
""" function implementation """
If inject
decorates a class, it will be applied to the __init__
method.
Especially useful for dataclasses:
Note: Doesn't work with Pydantic
BaseModel
because the signature of the__init__
method doesn't contain the dependencies.
from dataclasses import dataclass
from injection import inject
@inject
@dataclass
class SomeDataClass:
service_a: ServiceA = ...
Get an instance
Example with get_instance
function:
from injection import get_instance
service_a = get_instance(ServiceA)
Example with get_lazy_instance
function:
from injection import get_lazy_instance
lazy_service_a = get_lazy_instance(ServiceA)
# ...
service_a = ~lazy_service_a
Inheritance
In the case of inheritance, you can use the decorator parameter on
to link the injection to one or several other
classes.
Warning: if the child class is in another file, make sure that file is imported before injection.
See load_package
function.
Example with one class:
class AbstractService(ABC):
...
@injectable(on=AbstractService)
class ConcreteService(AbstractService):
...
Example with several classes:
class AbstractService(ABC):
...
class ConcreteService(AbstractService):
...
@injectable(on=(AbstractService, ConcreteService))
class ConcreteServiceOverload(ConcreteService):
...
If a class is registered in a package, and you want to override it, there is the mode
parameter:
@injectable
class InaccessibleService:
...
# ...
@injectable(on=InaccessibleService, mode="override")
class ServiceOverload(InaccessibleService):
...
Recipes
A recipe is a function that tells the injector how to construct the instance to be injected. It is important to specify the return type annotation when defining the recipe.
from injection import injectable
@injectable
def service_d_recipe() -> ServiceD:
""" recipe implementation """
Working with type aliases
from injection import injectable, set_constant
type APIKey = str
set_constant("<secret_api_key>", APIKey, alias=True)
@injectable
class Client:
def __init__(self, api_key: APIKey):
...
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
Hashes for python_injection-0.9.8.post0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44e00aad5ed61a7e6e2078aba651e7268a0426137b3a9fec3dbb3b2a1f2f0f66 |
|
MD5 | a107d480198a57e0cf422d700b121d85 |
|
BLAKE2b-256 | 689b88bfb9167c185c2bb9a1a789bbdca27d787a3fcb23ed89cd6473726eabbb |
Hashes for python_injection-0.9.8.post0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 671b5c824bd92e90743c72d4b9365e36a39cb7b770c216666a97ac8867881233 |
|
MD5 | 18d314092beab555bb802aa202dfb5a7 |
|
BLAKE2b-256 | ca7873912ebaaa3659a7744434f833dc34468c3ee595dcea3fcb58883aaf3577 |