Skip to content

Instantly share code, notes, and snippets.

@nerodono
Created December 18, 2023 05:22
Show Gist options
  • Save nerodono/654281d5ef56e63b6284292bd3edeb90 to your computer and use it in GitHub Desktop.
Save nerodono/654281d5ef56e63b6284292bd3edeb90 to your computer and use it in GitHub Desktop.
Adaptix tagged union MRE
from adaptix import Retort
from typing import Literal
from dataclasses import dataclass
from io import IOBase
@dataclass(frozen=False, slots=True)
class InputMediaVideo:
"""Represents a video to be sent."""
media: IOBase | str
"""File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass "attach://<file_attach_name>" to upload a new one using multipart/form-data under <file_attach_name> name. More information on Sending Files: https://core.telegram.org/bots/api#sending-files """
type: Literal["video"] = "video"
"""Type of the result, must be video """
thumbnail: IOBase | str | None = None
"""Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side. The thumbnail should be in JPEG format and less than 200 kB in size. A thumbnail's width and height should not exceed 320. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can't be reused and can be only uploaded as a new file, so you can pass "attach://<file_attach_name>" if the thumbnail was uploaded using multipart/form-data under <file_attach_name>. More information on Sending Files: https://core.telegram.org/bots/api#sending-files """
caption: str | None = None
"""Optional. Caption of the video to be sent, 0-1024 characters after entities parsing """
parse_mode: str | None = None
"""Optional. List of special entities that appear in the caption, which can be specified instead of parse_mode """
width: int | None = None
"""Optional. Video width """
height: int | None = None
"""Optional. Video height """
duration: int | None = None
"""Optional. Video duration in seconds """
supports_streaming: bool | None = None
"""Optional. Pass True if the uploaded video is suitable for streaming """
has_spoiler: bool | None = None
"""Optional. Pass True if the video needs to be covered with a spoiler animation """
@dataclass(frozen=False, slots=True)
class InputMediaPhoto:
"""Represents a photo to be sent."""
media: IOBase | str
"""File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass "attach://<file_attach_name>" to upload a new one using multipart/form-data under <file_attach_name> name. More information on Sending Files: https://core.telegram.org/bots/api#sending-files """
type: Literal["photo"] = "photo"
"""Type of the result, must be photo """ ""
@dataclass(frozen=False, slots=True)
class SendMediaGroup:
"""Use this method to send a group of photos, videos, documents or audios as an album. Documents and audio files can be only grouped in an album with messages of the same type. On success, an array of Messages that were sent is returned."""
chat_id: int | str
"""Unique identifier for the target chat or username of the target channel (in the format @channelusername) """
media: list[InputMediaPhoto] | list[InputMediaVideo]
"""A JSON-serialized array describing messages to be sent, must include 2-10 items """
message_thread_id: int | None = None
"""Unique identifier for the target message thread (topic) of the forum; for forum supergroups only """
disable_notification: bool | None = None
"""Sends messages silently. Users will receive a notification with no sound. """
protect_content: bool | None = None
"""Protects the contents of the sent messages from forwarding and saving """
reply_to_message_id: int | None = None
"""If the messages are a reply, ID of the original message """
allow_sending_without_reply: bool | None = None
"""Pass True if the message should be sent even if the specified replied-to message is not found """
retort = Retort()
retort.dump(
SendMediaGroup(
chat_id=1337,
media=[
InputMediaPhoto("hello world"),
InputMediaPhoto("Wow such a silly"),
],
),
SendMediaGroup,
)
+ Exception Group Traceback (most recent call last):
| File "/home/nero/Dev/Python/slonogram/examples/../trashcan/mre.py", line 69, in <module>
| retort.dump(
| File "/home/nero/Dev/Python/slonogram/.venv/lib/python3.11/site-packages/adaptix/_internal/facade/retort.py", line 320, in dump
| return self.get_dumper(tp)(data)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| File "<adaptix generated model_dumper_SendMediaGroup>", line 79, in model_dumper_SendMediaGroup
| ExceptionGroup: while dumping model <class '__main__.SendMediaGroup'> (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "<adaptix generated model_dumper_SendMediaGroup>", line 49, in model_dumper_SendMediaGroup
| File "/home/nero/Dev/Python/slonogram/.venv/lib/python3.11/site-packages/adaptix/_internal/morphing/generic_provider.py", line 301, in union_dumper
| return dumper_type_dispatcher.dispatch(type(data))(data)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/nero/Dev/Python/slonogram/.venv/lib/python3.11/site-packages/adaptix/_internal/morphing/iterable_provider.py", line 278, in iter_dt_dumper
| return iter_factory(iter_dumper(data))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/home/nero/Dev/Python/slonogram/.venv/lib/python3.11/site-packages/adaptix/_internal/morphing/iterable_provider.py", line 269, in iter_dumper_dt_all
| raise CompatExceptionGroup(
| ExceptionGroup: while dumping iterable <class 'list'> (2 sub-exceptions)
| Exception was caused at [Attr('media')]
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "/home/nero/Dev/Python/slonogram/.venv/lib/python3.11/site-packages/adaptix/_internal/morphing/iterable_provider.py", line 262, in iter_dumper_dt_all
| yield dumper(el)
| ^^^^^^^^^^
| File "<adaptix generated model_dumper_InputMediaVideo>", line 106, in model_dumper_InputMediaVideo
| ExceptionGroup: while dumping model <class '__main__.InputMediaVideo'> (8 sub-exceptions)
| Exception was caused at [0]
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 66, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'thumbnail'
| Exception was caused at [Attr('thumbnail')]
+---------------- 2 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 71, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'caption'
| Exception was caused at [Attr('caption')]
+---------------- 3 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 76, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'parse_mode'
| Exception was caused at [Attr('parse_mode')]
+---------------- 4 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 81, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'width'
| Exception was caused at [Attr('width')]
+---------------- 5 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 86, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'height'
| Exception was caused at [Attr('height')]
+---------------- 6 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 91, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'duration'
| Exception was caused at [Attr('duration')]
+---------------- 7 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 96, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'supports_streaming'
| Exception was caused at [Attr('supports_streaming')]
+---------------- 8 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 101, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'has_spoiler'
| Exception was caused at [Attr('has_spoiler')]
+------------------------------------
+---------------- 2 ----------------
| Exception Group Traceback (most recent call last):
| File "/home/nero/Dev/Python/slonogram/.venv/lib/python3.11/site-packages/adaptix/_internal/morphing/iterable_provider.py", line 262, in iter_dumper_dt_all
| yield dumper(el)
| ^^^^^^^^^^
| File "<adaptix generated model_dumper_InputMediaVideo>", line 106, in model_dumper_InputMediaVideo
| ExceptionGroup: while dumping model <class '__main__.InputMediaVideo'> (8 sub-exceptions)
| Exception was caused at [1]
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 66, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'thumbnail'
| Exception was caused at [Attr('thumbnail')]
+---------------- 2 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 71, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'caption'
| Exception was caused at [Attr('caption')]
+---------------- 3 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 76, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'parse_mode'
| Exception was caused at [Attr('parse_mode')]
+---------------- 4 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 81, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'width'
| Exception was caused at [Attr('width')]
+---------------- 5 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 86, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'height'
| Exception was caused at [Attr('height')]
+---------------- 6 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 91, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'duration'
| Exception was caused at [Attr('duration')]
+---------------- 7 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 96, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'supports_streaming'
| Exception was caused at [Attr('supports_streaming')]
+---------------- 8 ----------------
| Traceback (most recent call last):
| File "<adaptix generated model_dumper_InputMediaVideo>", line 101, in model_dumper_InputMediaVideo
| AttributeError: 'InputMediaPhoto' object has no attribute 'has_spoiler'
| Exception was caused at [Attr('has_spoiler')]
+------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment