aiogram/aiogram/dispatcher/handler/base.py
Alex Root Junior e4046095d7
Dev 3.x i18n & improvements (#696)
* Added base code and make code improvements
* Auto-exclude coverage for `if TYPE_CHECKING:`
* Fixed current coverage
* Cover I18n module
* Update pipeline
* Fixed annotations
* Added docs
* Move exceptions
* Added tests for KeyboardBuilder and initial docs
* Remove help generator (removed from sources tree, requires rewrite)
* Added patch-notes #698, #699, #700, #701, #702, #703
2021-09-22 00:52:38 +03:00

40 lines
984 B
Python

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, Generic, TypeVar, cast
from aiogram import Bot
from aiogram.types import Update
T = TypeVar("T")
class BaseHandlerMixin(Generic[T]):
if TYPE_CHECKING:
event: T
data: Dict[str, Any]
class BaseHandler(BaseHandlerMixin[T], ABC):
"""
Base class for all class-based handlers
"""
def __init__(self, event: T, **kwargs: Any) -> None:
self.event: T = event
self.data: Dict[str, Any] = kwargs
@property
def bot(self) -> Bot:
if "bot" in self.data:
return cast(Bot, self.data["bot"])
return Bot.get_current(no_error=False)
@property
def update(self) -> Update:
return cast(Update, self.data.get("update", self.data.get("event_update")))
@abstractmethod
async def handle(self) -> Any: # pragma: no cover
pass
def __await__(self) -> Any:
return self.handle().__await__()