Доработано преобразование вложений в Pydantic модели

This commit is contained in:
Денис Семёнов 2025-08-02 23:22:36 +03:00
parent 5bc5fb45c8
commit 3fa34079ae
10 changed files with 38 additions and 33 deletions

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Literal, Optional
from ...enums.attachment import AttachmentType
@ -15,5 +15,5 @@ class Audio(Attachment):
transcription (Optional[str]): Транскрипция аудио (если есть).
"""
type: AttachmentType = AttachmentType.AUDIO
type: Literal[AttachmentType.AUDIO]
transcription: Optional[str] = None

View File

@ -1,10 +1,11 @@
from typing import Literal
from pydantic import BaseModel
from ..attachment import ButtonsPayload
from ....enums.attachment import AttachmentType
from ..attachment import Attachment
class AttachmentButton(BaseModel):
class AttachmentButton(Attachment):
"""
Модель кнопки вложения для сообщения.
@ -14,5 +15,4 @@ class AttachmentButton(BaseModel):
payload: Полезная нагрузка кнопки (массив рядов кнопок)
"""
type: Literal['inline_keyboard'] = 'inline_keyboard'
payload: ButtonsPayload
type: Literal[AttachmentType.INLINE_KEYBOARD]

View File

@ -1,3 +1,4 @@
from typing import Literal
from ...enums.attachment import AttachmentType
from .attachment import Attachment
@ -12,4 +13,4 @@ class Contact(Attachment):
type (Literal['contact']): Тип вложения, всегда 'contact'.
"""
type: AttachmentType = AttachmentType.CONTACT
type: Literal[AttachmentType.CONTACT]

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Literal, Optional
from ...enums.attachment import AttachmentType
@ -16,6 +16,6 @@ class File(Attachment):
size (Optional[int]): Размер файла в байтах.
"""
type: AttachmentType = AttachmentType.FILE
type: Literal[AttachmentType.FILE]
filename: Optional[str] = None
size: Optional[int] = None

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Literal, Optional
from pydantic import BaseModel
@ -31,4 +31,4 @@ class Image(Attachment):
type (Literal['image']): Тип вложения, всегда 'image'.
"""
type: AttachmentType = AttachmentType.IMAGE
type: Literal[AttachmentType.IMAGE]

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Literal, Optional
from ...enums.attachment import AttachmentType
@ -16,6 +16,6 @@ class Location(Attachment):
longitude (Optional[float]): Долгота.
"""
type: AttachmentType = AttachmentType.LOCATION
type: Literal[AttachmentType.LOCATION]
latitude: Optional[float] = None
longitude: Optional[float] = None

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Literal, Optional
from ...enums.attachment import AttachmentType
@ -17,7 +17,7 @@ class Share(Attachment):
image_url (Optional[str]): URL изображения для предпросмотра.
"""
type: AttachmentType = AttachmentType.SHARE
type: Literal[AttachmentType.SHARE]
title: Optional[str] = None
description: Optional[str] = None
image_url: Optional[str] = None

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Literal, Optional
from ...enums.attachment import AttachmentType
@ -16,6 +16,6 @@ class Sticker(Attachment):
height (Optional[int]): Высота стикера в пикселях.
"""
type: AttachmentType = AttachmentType.STICKER
type: Literal[AttachmentType.STICKER]
width: Optional[int] = None
height: Optional[int] = None

View File

@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any, Literal, Optional
from pydantic import BaseModel, Field
from ...enums.attachment import AttachmentType
@ -61,7 +61,7 @@ class Video(Attachment):
bot (Optional[Any]): Ссылка на экземпляр бота, не сериализуется.
"""
type: AttachmentType = AttachmentType.VIDEO
type: Literal[AttachmentType.VIDEO]
token: Optional[str] = None
urls: Optional[VideoUrl] = None
thumbnail: VideoThumbnail

View File

@ -1,7 +1,9 @@
from __future__ import annotations
from pydantic import BaseModel, Field
from typing import Any, Optional, List, Union, TYPE_CHECKING
from typing import Annotated, Any, Optional, List, Union, TYPE_CHECKING
from ..types.attachments.contact import Contact
from ..enums.text_style import TextStyle
from ..enums.parse_mode import ParseMode
@ -24,6 +26,19 @@ from .users import User
if TYPE_CHECKING:
from ..bot import Bot
from ..types.input_media import InputMedia, InputMediaBuffer
Attachments = Annotated[Union[
Audio,
Video,
File,
Image,
Sticker,
Share,
Location,
AttachmentButton,
Contact
], Field(discriminator='type')]
class MarkupElement(BaseModel):
@ -91,18 +106,7 @@ class MessageBody(BaseModel):
seq: int
text: Optional[str] = None
attachments: Optional[
List[
Union[
AttachmentButton,
Audio,
Video,
File,
Image,
Sticker,
Share,
Location
]
]
List[Attachments]
] = Field(default_factory=list) # type: ignore
markup: Optional[