Refactor: Introduce Union types for streamlined type handling (#1649)

* Refactor: Introduce Union types for streamlined type handling

Implemented Union types across various modules to consolidate and simplify type annotations. This change replaces repetitive union declarations with reusable Union aliases, improving code readability and maintainability. Updates applied to affected classes, methods, and imports accordingly.

* Refactor unions into type aliases for better reusability

Replaced inline `Union` types with predefined aliases like `MediaUnion`, `ReplyMarkupUnion`, and `ChatIdUnion`. Simplifies type annotations, improves code readability, and reduces duplication. Added `media_union.py` for grouping related media types.

* Refactor type unions with ResultChatMemberUnion and ResultMenuButtonUnion

Replaced verbose type definitions of chat member and menu button unions with `ResultChatMemberUnion` and `ResultMenuButtonUnion` for improved readability and maintainability. Updated relevant methods, modules, and documentation to use the new type aliases consistently.

* Added changelog
This commit is contained in:
Alex Root Junior 2025-03-08 02:19:57 +02:00 committed by GitHub
parent 843c1dec7c
commit cd4e811856
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
173 changed files with 1452 additions and 2562 deletions

View file

@ -6,13 +6,12 @@ from pydantic import Field
from ..client.default import Default
from ..types import (
ForceReply,
InlineKeyboardMarkup,
ChatIdUnion,
InputFile,
InputFileUnion,
Message,
MessageEntity,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
ReplyMarkupUnion,
ReplyParameters,
)
from .base import TelegramMethod
@ -28,9 +27,9 @@ class SendDocument(TelegramMethod[Message]):
__returning__ = Message
__api_method__ = "sendDocument"
chat_id: Union[int, str]
chat_id: ChatIdUnion
"""Unique identifier for the target chat or username of the target channel (in the format :code:`@channelusername`)"""
document: Union[InputFile, str]
document: InputFileUnion
"""File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. :ref:`More information on Sending Files » <sending-files>`"""
business_connection_id: Optional[str] = None
"""Unique identifier of the business connection on behalf of which the message will be sent"""
@ -56,9 +55,7 @@ class SendDocument(TelegramMethod[Message]):
"""Unique identifier of the message effect to be added to the message; for private chats only"""
reply_parameters: Optional[ReplyParameters] = None
"""Description of the message to reply to"""
reply_markup: Optional[
Union[InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply]
] = None
reply_markup: Optional[ReplyMarkupUnion] = None
"""Additional interface options. A JSON-serialized object for an `inline keyboard <https://core.telegram.org/bots/features#inline-keyboards>`_, `custom reply keyboard <https://core.telegram.org/bots/features#keyboards>`_, instructions to remove a reply keyboard or to force a reply from the user"""
allow_sending_without_reply: Optional[bool] = Field(
None, json_schema_extra={"deprecated": True}
@ -80,8 +77,8 @@ class SendDocument(TelegramMethod[Message]):
def __init__(
__pydantic__self__,
*,
chat_id: Union[int, str],
document: Union[InputFile, str],
chat_id: ChatIdUnion,
document: InputFileUnion,
business_connection_id: Optional[str] = None,
message_thread_id: Optional[int] = None,
thumbnail: Optional[InputFile] = None,
@ -94,9 +91,7 @@ class SendDocument(TelegramMethod[Message]):
allow_paid_broadcast: Optional[bool] = None,
message_effect_id: Optional[str] = None,
reply_parameters: Optional[ReplyParameters] = None,
reply_markup: Optional[
Union[InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply]
] = None,
reply_markup: Optional[ReplyMarkupUnion] = None,
allow_sending_without_reply: Optional[bool] = None,
reply_to_message_id: Optional[int] = None,
**__pydantic_kwargs: Any,