wired_injector¶
Tired of picking apart the wired container inside your data classes and services?
wired_injector
brings type-based dependency injection, plus a pipeline with operators, to your code.
Installation¶
Installation follows the normal Python packaging approach:
$ pip install wired_injector
Quick Examples¶
Make an injectable factory:
@injectable()
@dataclass
class View:
name: str = 'View'
You can then make an InjectorRegistry
that scans for decorators:
registry = InjectorRegistry()
registry.scan() # Look for decorators in/below current package
The inject can construct your instances.
Maybe you need the container’s Settings
:
@injectable()
@dataclass
class View:
settings: Settings
Want to use NamedTuple
instead of dataclass
?
@injectable()
class View(NamedTuple):
settings: Settings
Or perhaps functions instead?
@injectable()
def View(settings: Settings):
site_name = settings.site_name
name = f'View - {site_name}'
return dict(name=name)
Use Annotated
to give instructions to the injector, such as look up one type but expect a return value of another:
settings: Annotated[MySettings, Get(BaseSettings)]
Use pipelines with operators – even custom – to narrow down the injection to the smallest surface area:
site_name: Annotated[
str,
Get(BaseSettings),
Attr('site_name'),
]