refactor(telegram): boundary types (#90)

This commit is contained in:
banteg
2026-01-11 21:36:07 +04:00
committed by GitHub
parent c6c34ac17f
commit e8c478d786
23 changed files with 1116 additions and 581 deletions
+14 -12
View File
@@ -3,6 +3,7 @@ from typing import Any
import anyio
import pytest
from takopi.telegram.api_models import File, Message, Update, User
from takopi.telegram.client import BotClient, TelegramClient, TelegramRetryAfter
@@ -29,7 +30,7 @@ class _FakeBot(BotClient):
reply_markup: dict | None = None,
*,
replace_message_id: int | None = None,
) -> dict[str, Any]:
) -> Message | None:
_ = reply_to_message_id
_ = disable_notification
_ = message_thread_id
@@ -38,7 +39,7 @@ class _FakeBot(BotClient):
_ = reply_markup
_ = replace_message_id
self.calls.append("send_message")
return {"message_id": 1}
return Message(message_id=1)
async def send_document(
self,
@@ -49,7 +50,7 @@ class _FakeBot(BotClient):
message_thread_id: int | None = None,
disable_notification: bool | None = False,
caption: str | None = None,
) -> dict[str, Any]:
) -> Message | None:
_ = (
chat_id,
filename,
@@ -60,7 +61,7 @@ class _FakeBot(BotClient):
caption,
)
self.calls.append("send_document")
return {"message_id": 1}
return Message(message_id=1)
async def edit_message_text(
self,
@@ -72,7 +73,7 @@ class _FakeBot(BotClient):
reply_markup: dict | None = None,
*,
wait: bool = True,
) -> dict[str, Any]:
) -> Message | None:
_ = chat_id
_ = message_id
_ = entities
@@ -85,7 +86,7 @@ class _FakeBot(BotClient):
self._edit_attempts += 1
raise TelegramRetryAfter(self.retry_after)
self._edit_attempts += 1
return {"message_id": message_id}
return Message(message_id=message_id)
async def delete_message(
self,
@@ -113,7 +114,7 @@ class _FakeBot(BotClient):
offset: int | None,
timeout_s: int = 50,
allowed_updates: list[str] | None = None,
) -> list[dict[str, Any]] | None:
) -> list[Update] | None:
_ = offset
_ = timeout_s
_ = allowed_updates
@@ -123,7 +124,7 @@ class _FakeBot(BotClient):
self._updates_attempts += 1
return []
async def get_file(self, file_id: str) -> dict[str, Any] | None:
async def get_file(self, file_id: str) -> File | None:
_ = file_id
return None
@@ -134,8 +135,8 @@ class _FakeBot(BotClient):
async def close(self) -> None:
return None
async def get_me(self) -> dict[str, Any] | None:
return {"id": 1}
async def get_me(self) -> User | None:
return User(id=1)
async def answer_callback_query(
self,
@@ -187,7 +188,7 @@ async def test_edits_coalesce_latest() -> None:
reply_markup: dict | None = None,
*,
wait: bool = True,
) -> dict:
) -> Message | None:
if self._block_first:
self._block_first = False
self.edit_started.set()
@@ -305,7 +306,8 @@ async def test_retry_after_retries_once() -> None:
text="retry",
)
assert result == {"message_id": 1}
assert result is not None
assert result.message_id == 1
assert bot._edit_attempts == 2