Modern-DI integration for aiogram 3.x.
Full guide: aiogram integration docs
Usage example: examples/
Installation
uv add modern-di-aiogram # or: pip install modern-di-aiogram
Usage
aiogram has no dependency-injection system of its own, so modern-di-aiogram pairs an @inject decorator with inert FromDI markers (or auto_inject=True to skip the decorator entirely). setup_di opens the root container on dispatcher startup, closes it on shutdown, and installs an outer middleware that builds a per-update Scope.REQUEST child container automatically.
import typing
from aiogram import Dispatcher
from aiogram.types import Message
from modern_di import Container, Group, Scope, providers
from modern_di_aiogram import FromDI, inject, setup_di
class Settings:
def __init__(self) -> None:
self.greeting = "hello"
class AppGroup(Group):
settings = providers.Factory(Settings, scope=Scope.APP, cache=True)
dispatcher = Dispatcher()
container = Container(groups=[AppGroup])
setup_di(dispatcher, container)
container.validate() # optional fail-fast; must come after setup_di registers its providers
@dispatcher.message()
@inject
async def greet(
message: Message,
settings: typing.Annotated[Settings, FromDI(AppGroup.settings)],
) -> None:
await message.answer(f"{settings.greeting}, {message.from_user.first_name}")
Pass auto_inject=True to setup_di to wrap every handler already registered on the dispatcher, so individual handlers don't need @inject — register handlers before startup for this to take effect. The current aiogram.types.Update and the concrete event it carries (Message, CallbackQuery, …) are resolvable within DI via the pre-built aiogram_update_provider / aiogram_event_provider context providers. aiogram-dialog getters and callbacks are supported via modern_di_aiogram.dialog — see the docs.
API
| Symbol | Description |
|---|---|
setup_di(dispatcher, container, *, auto_inject=False) |
Stores the container on the dispatcher, registers the update/event providers, wires dispatcher.startup/dispatcher.shutdown to open/close it, and installs the per-update middleware. With auto_inject=True, also wraps every handler already registered at startup |
FromDI(dependency) |
Inert marker (used with @inject) that resolves a provider or type from the per-update child container |
inject(handler) |
Decorator for an aiogram handler; resolves its FromDI-annotated parameters. Not needed when setup_di(..., auto_inject=True) is used. Raises RuntimeError naming setup_di when an update reaches it without the middleware installed |
fetch_di_container(dispatcher) |
Returns the root Container stored on the dispatcher |
aiogram_update_provider |
ContextProvider for the current aiogram.types.Update (REQUEST scope) |
aiogram_event_provider |
ContextProvider for the current aiogram.types.TelegramObject (REQUEST scope) — the concrete event unwrapped from the Update |
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with IoC container and scopes.
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.
Metadata
Release files for modern-di-aiogram 3.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_aiogram-3.0.3.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_aiogram-3.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / modern_di_aiogram-3.0.3.tar.gz
| Download URL | modern_di_aiogram-3.0.3.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1eb1715524a79e81ab6d3a781cefb067728c98bdbdeee086b4745d082c65d0a6
|
|
BLAKE2b-256 checksum How to use checksums |
837bc99aa477f8a8260a45e95ccf41c71affe8a14ff3c49896f1bad008c9b2c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / modern_di_aiogram-3.0.3-py3-none-any.whl
| Download URL | modern_di_aiogram-3.0.3-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b31612d7a91005fb4fd8e2129af2dc3467c238d6276433f71d1b92017e35ad0
|
|
BLAKE2b-256 checksum How to use checksums |
2e840755ce43c137b5d78af5059ee371a4f311b91a4784816c998793ba67a156
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|