mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Add context manager support for bot client (#1468)
* Add context manager support for bot client The bot client now supports the context manager protocol, providing automatic resource management. This enhancement helps to automatically close the session when leaving the context, which cleans up resources better. The documentation and tests have been updated accordingly to illustrate this new feature. Moreover, an example of usage without a dispatcher has been provided to clarify its use in simple cases. * Added changelog
This commit is contained in:
parent
9756dac877
commit
4729978c60
5 changed files with 84 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import io
|
|||
import pathlib
|
||||
import warnings
|
||||
from contextlib import asynccontextmanager
|
||||
from types import TracebackType
|
||||
from typing import (
|
||||
Any,
|
||||
AsyncGenerator,
|
||||
|
|
@ -12,6 +13,7 @@ from typing import (
|
|||
BinaryIO,
|
||||
List,
|
||||
Optional,
|
||||
Type,
|
||||
TypeVar,
|
||||
Union,
|
||||
cast,
|
||||
|
|
@ -300,6 +302,17 @@ class Bot:
|
|||
self.__token = token
|
||||
self._me: Optional[User] = None
|
||||
|
||||
async def __aenter__(self) -> "Bot":
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: Optional[Type[BaseException]],
|
||||
exc_value: Optional[BaseException],
|
||||
traceback: Optional[TracebackType],
|
||||
) -> None:
|
||||
await self.session.close()
|
||||
|
||||
@property
|
||||
def parse_mode(self) -> Optional[str]:
|
||||
warnings.warn(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue