Skip to main content

modern-di-aiogram

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

Modern-DI integration for aiogram 3.x.

Full guide: aiogram integration docs

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()
setup_di(dispatcher, Container(groups=[AppGroup], validate=True))


@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
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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern_di_aiogram-3.0.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di_aiogram-3.0.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_aiogram-3.0.0.tar.gz.

File metadata

  • Download URL: modern_di_aiogram-3.0.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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}

File hashes

Hashes for modern_di_aiogram-3.0.0.tar.gz
Algorithm Hash digest
SHA256 4f73c16d56afccdae10f3e0cc8e18f1ed24c646f1091b5d4e2009be2afd22bf1
MD5 70cbf83937b21d60593d360cef3f178c
BLAKE2b-256 81f5aae4846674c1dea4b69f2ff8e9ea301719a00d75beff49c9775eb33d9b23

See more details on using hashes here.

File details

Details for the file modern_di_aiogram-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: modern_di_aiogram-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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}

File hashes

Hashes for modern_di_aiogram-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb2e5f8484eb3547b05ddcd3bbe0c000b75d3592038d8da4333a0925140f0245
MD5 f7645f4dbf0fcdec34fe3d4c8d1c0374
BLAKE2b-256 fc92f9314a8c6ce71886d3123cf5b60a3f7a5758a4197e3f3a3ebe8351c1f13e

See more details on using hashes here.

Release history Release notifications | RSS feed

3.0.1

2 files

This release

3.0.0 This release

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page