Typed data (#1647)
Some checks failed
Tests / tests (macos-latest, 3.10) (push) Has been cancelled
Tests / tests (macos-latest, 3.11) (push) Has been cancelled
Tests / tests (macos-latest, 3.12) (push) Has been cancelled
Tests / tests (macos-latest, 3.13) (push) Has been cancelled
Tests / tests (macos-latest, 3.9) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.10) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.13) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.9) (push) Has been cancelled
Tests / tests (windows-latest, 3.10) (push) Has been cancelled
Tests / tests (windows-latest, 3.11) (push) Has been cancelled
Tests / tests (windows-latest, 3.12) (push) Has been cancelled
Tests / tests (windows-latest, 3.13) (push) Has been cancelled
Tests / tests (windows-latest, 3.9) (push) Has been cancelled
Tests / pypy-tests (macos-latest, pypy3.10) (push) Has been cancelled
Tests / pypy-tests (macos-latest, pypy3.9) (push) Has been cancelled
Tests / pypy-tests (ubuntu-latest, pypy3.10) (push) Has been cancelled
Tests / pypy-tests (ubuntu-latest, pypy3.9) (push) Has been cancelled

* Add TypedDict support for middleware context data

Introduced `MiddlewareData` and associated TypedDicts to type-hint middleware context data. Updated documentation to include usage examples and guidelines for extending the default middleware data. Also adjusted coverage configuration to exclude the new data module.

* Added more docstrings

* Typo fixes
This commit is contained in:
Alex Root Junior 2025-03-02 01:24:30 +02:00 committed by GitHub
parent 8b4976b3de
commit 843c1dec7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 161 additions and 1 deletions

View file

@ -70,3 +70,61 @@ The last way is to return a dictionary from the filter:
.. literalinclude:: ../../examples/context_addition_from_filter.py
...or using :ref:`MagicFilter <magic-filters>` with :code:`.as_(...)` method.
Using type hints
================
.. note::
Type-hinting middleware data is optional and is not required for the correct operation of the dispatcher.
However, it is recommended to use it to improve the readability of the code.
You can use type hints to specify the type of the context data in the middlewares, filters and handlers.
The default middleware data typed dict can be found in :class:`aiogram.dispatcher.middlewares.data.MiddlewareData`.
In case when you have extended the context data, you can use the :class:`aiogram.dispatcher.middlewares.data.MiddlewareData` as a base class and specify the type hints for the new fields.
.. warning::
If you using type checking tools like mypy, you can experience warnings about that this type hint against Liskov substitution principle in due stricter type is not a subclass of :code:`dict[str, Any]`.
This is a known issue and it is not a bug. You can ignore this warning or use :code:`# type: ignore` comment.
Example of using type hints:
.. code-block:: python
from aiogram.dispatcher.middlewares.data import MiddlewareData
class MyMiddlewareData(MiddlewareData, total=False):
my_custom_value: int
class MyMessageMiddleware(BaseMiddleware):
async def __call__(
self,
handler: Callable[[Message, MyMiddlewareData], Awaitable[Any]],
event: Message,
data: MyMiddlewareData,
) -> Any:
bot = data["bot"] # <-- IDE will show you that data has `bot` key and its type is `Bot`
data["my_custom_value"] = bot.id * 42 # <-- IDE will show you that you can set `my_custom_value` key with int value and warn you if you try to set it with other type
return await handler(event, data)
Available context data type helpers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: aiogram.dispatcher.middlewares.data.MiddlewareData
:members:
:undoc-members:
:member-order: bysource
.. autoclass:: aiogram.dispatcher.middlewares.data.I18nData
:members:
:undoc-members:
:member-order: bysource