Fast and easy dependency injection framework.
Project description
Injection
Fast and easy dependency injection framework.
Quick start
⚠️ Requires Python 3.10 or higher
pip install python-injection
How to use
Create an injectable
If you wish to inject a singleton, use unique
decorator.
from injection import unique
@unique
class MyClass:
""" class implementation """
If you wish to inject a new instance each time, use new
decorator.
from injection import new
@new
class MyClass:
""" 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 my_function(instance: MyClass):
""" function implementation """
Inheritance
In the case of inheritance, you can use the decorator parameters reference
or references
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.
reference
parameter example:
from injection import unique
class A:
...
@unique(reference=A)
class B(A):
...
references
parameter example:
from injection import unique
class A:
...
class B(A):
...
@unique(references=(A, B))
class C(B):
...
Recipes
A recipe is a function that tells the injector how to construct the instance to be injected. It is important to specify the reference class(es) when defining the recipe.
from injection import unique
@unique(reference=MyClass)
def my_recipe() -> MyClass:
""" recipe implementation """
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.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4941210cd5f7fc69890333398e4cc39de8196b8e8b2ad997084901ccec93bcdd |
|
MD5 | 92e77900d9fe3a4edf655a38a24c8c27 |
|
BLAKE2b-256 | 80841558b00df5ea4b29a1f39a87892cc385a2b041b760cf8dfef51fa815f226 |