Python-Diana
A simple async-friendly dependency injection framework for Python 3.
- Supports:
Async methods
Parametrized dependencies
Type annotations
Simple Example
import diana
class MyThing(object):
def __init__(self, prefix):
self.prefix = prefix
def get(self, suffix):
return self.prefix + suffix
class MyModule(diana.Module):
@diana.provider
def provide_thing(self) -> MyThing:
return MyThing("a_prefix_")
@diana.injector
def requires_a_thing(*, thing: MyThing):
return thing.get('suffix')
diana.injector.load_module(MyModule())
requires_a_thing() # returns "a_prefix_suffix"
Parametrized Dependencies
import diana
import typing
Snake = typing.NewType('Snake', str)
class SnakeModule(diana.Module):
@diana.provider
def provide_snake(self, length: int) -> Snake:
return Snake('-' + ('=' * length) + e)
@diana.injector(a_snake=Snake)
@diana.injector.param('a_snake', length=5)
def snake_printer(a_snake):
print(a_snake, "Hissss")
diana.injector.load_module(SnakeModule())
snake_printer() # Prints: -=====e Hissss
Modules
Modules provide dependencies.
class MyModule(diana.Module):
def load(self, injector):
# Called when the module is loaded against `injector`.
pass
def unload(self, injector):
# Called when the module is unloaded against `injector`.
pass
@diana.provider
def provide_atype(self) -> AType:
return AType()
@diana.provider
async def provide_atype_async(self) -> AType:
await async_stuff()
return AType()
@diana.provides(BType)
def provide_btype(self):
return BType()
@MyModule.provider
def provide_ctype(module) -> CType:
return CType()
@MyModule.provides(DType)
def provide_dtype(module):
return DType()
Injection Styles
There are three formats for injecting dependencies into functions
Type Annotation w/ inspect
Explicit
Parametrized
The following examples are all equivalent.
# Type Annotation w/ Inspect
@diana.injector
def func_a(*, a: AType, b: BType):
pass
# Explicit
@diana.injector.inject(a=AType, b=BType)
def func_a(*, a, b):
pass
# Parametrized.
# Note, if the second argument to param() is omitted, the type must be
# hinted by one of the previous methods.
@diana.injector.param('a', AType, a_param=...)
@diana.injector.param('b', BType, b_param=...)
def func_a(*, a, b):
pass
In all cases, injected arguments must be keyword-only.
Alternatively, a dependency can be manually provided, bypassing any injection.
func_a(a=AType(), b=BType())
Missing Features
Compared to other dependency injection frameworks, a few features are missing.
Scope management - Currently there is no provision for scope to be managed by diana and remains the Module/provider’s responsibility.
Constructor/Instance Injecting - it is not possible to have Diana set attributes on instances by decorating the class definition.
Thread safety - There have been no attempts to make Diana thread safe. In theory, if modules are only loaded once (presumably at runtime), thread safety can be managed by the Modules/providers.
Metadata
Release files for diana 3.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| diana-3.1.2-py3-none-any.whl | Python 3 | none | any | Details for diana-3.1.2-py3-none-any.whl |
Release files / diana-3.1.2-py3-none-any.whl
| Download URL | diana-3.1.2-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b542d1077263104d83f3e41d2723fc405bd33b3e05192236d6862f53b60b2a13
|
|
BLAKE2b-256 checksum How to use checksums |
a706cad17cbce0da026fdb718937f04d4f8f34bf15caa2e3273d655ee2c13c57
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.1
|
Security for diana 3.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.