mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
refactor(generic):
get rid of generic behaviour for base session
This commit is contained in:
parent
69b24c6203
commit
2d03285e88
4 changed files with 13 additions and 15 deletions
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import datetime
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, AsyncIterator, List, Optional, TypeVar, Union, cast
|
||||
from typing import Any, AsyncIterator, List, Optional, TypeVar, Union
|
||||
|
||||
from async_lru import alru_cache
|
||||
|
||||
|
|
@ -110,7 +110,7 @@ from ..types import (
|
|||
WebhookInfo,
|
||||
)
|
||||
from .session.aiohttp import AiohttpSession
|
||||
from .session.base import PT, BaseSession
|
||||
from .session.base import BaseSession
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
|
@ -121,15 +121,12 @@ class Bot(ContextInstanceMixin["Bot"]):
|
|||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
token: str,
|
||||
session: Optional[BaseSession[PT]] = None,
|
||||
parse_mode: Optional[str] = None,
|
||||
self, token: str, session: Optional[BaseSession] = None, parse_mode: Optional[str] = None,
|
||||
) -> None:
|
||||
validate_token(token)
|
||||
|
||||
if session is None:
|
||||
session = cast(BaseSession[PT], AiohttpSession())
|
||||
session = AiohttpSession()
|
||||
|
||||
self.session = session
|
||||
self.parse_mode = parse_mode
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ def _prepare_connector(chain_or_plain: _ProxyType) -> Tuple[Type["TCPConnector"]
|
|||
return ChainProxyConnector, dict(proxy_infos=infos)
|
||||
|
||||
|
||||
class AiohttpSession(BaseSession[_ProxyType]):
|
||||
class AiohttpSession(BaseSession):
|
||||
def __init__(
|
||||
self,
|
||||
api: TelegramAPIServer = PRODUCTION,
|
||||
|
|
@ -88,7 +88,9 @@ class AiohttpSession(BaseSession[_ProxyType]):
|
|||
|
||||
if self.proxy:
|
||||
try:
|
||||
self._connector_type, self._connector_init = _prepare_connector(self.proxy)
|
||||
self._connector_type, self._connector_init = _prepare_connector(
|
||||
cast(_ProxyType, self.proxy)
|
||||
)
|
||||
except ImportError as exc: # pragma: no cover
|
||||
raise UserWarning(
|
||||
"In order to use aiohttp client for proxy requests, install "
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import abc
|
|||
import datetime
|
||||
import json
|
||||
from types import TracebackType
|
||||
from typing import Any, AsyncGenerator, Callable, Generic, Optional, Type, TypeVar, Union
|
||||
from typing import Any, AsyncGenerator, Callable, Optional, Type, TypeVar, Union
|
||||
|
||||
from aiogram.utils.exceptions import TelegramAPIError
|
||||
|
||||
|
|
@ -15,7 +15,7 @@ T = TypeVar("T")
|
|||
PT = TypeVar("PT")
|
||||
|
||||
|
||||
class BaseSession(abc.ABC, Generic[PT]):
|
||||
class BaseSession(abc.ABC):
|
||||
def __init__(
|
||||
self,
|
||||
api: Optional[TelegramAPIServer] = None,
|
||||
|
|
@ -74,7 +74,7 @@ class BaseSession(abc.ABC, Generic[PT]):
|
|||
return {k: self.clean_json(v) for k, v in value.items() if v is not None}
|
||||
return value
|
||||
|
||||
async def __aenter__(self) -> BaseSession[PT]:
|
||||
async def __aenter__(self) -> BaseSession:
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import datetime
|
||||
import types
|
||||
from typing import Any, AsyncContextManager, AsyncGenerator
|
||||
from typing import AsyncContextManager, AsyncGenerator
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -14,7 +13,7 @@ except ImportError:
|
|||
from unittest.mock import AsyncMock as CoroutineMock, patch # type: ignore
|
||||
|
||||
|
||||
class CustomSession(BaseSession[Any]):
|
||||
class CustomSession(BaseSession):
|
||||
async def close(self):
|
||||
pass
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue