MessageID, InlineQueryResultBase changed as in API - #2618
Conversation
MessageID left as deprecated for compatibility. MaybeInaccessibleMessage added obviously as Union to conform API.
There was a problem hiding this comment.
Pull request overview
Renames MessageID to API-aligned MessageId while retaining a deprecated compatibility class.
Changes:
- Updates synchronous and asynchronous return types.
- Adds
MaybeInaccessibleMessageunion. - Updates type tests.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
telebot/types.py |
Adds renamed and compatibility types. |
telebot/__init__.py |
Uses MessageId in synchronous APIs. |
telebot/async_telebot.py |
Uses MessageId in asynchronous APIs. |
tests/test_types.py |
Tests MessageId deserialization. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.
Suppressed comments (3)
telebot/types.py:972
MessageIDis retained as a subclass of the new type, but results now created asMessageIdare not instances ofMessageID. For example, the updatedcopy_messagereturn path breaks existingisinstance(result, types.MessageID)checks, so this does not fully provide the compatibility stated in the PR description. Use an alias or arrange the hierarchy soMessageIdinstances also satisfy the deprecated name while preserving the desired warning behavior.
class MessageID(MessageId):
telebot/types.py:4858
- The deprecated base no longer acts as a compatibility type: every existing non-cached result class was changed to inherit
InlineQueryResultdirectly, soisinstance(InlineQueryResultArticle(...), InlineQueryResultBase)changes from true to false. Keep the old name as an alias or place it above the new class in the inheritance chain so existing base-class checks and type annotations continue to work during deprecation.
class InlineQueryResultBase(InlineQueryResult):
telebot/types.py:4795
- This new public base claims to represent all 20 inline-query result types and is now used by API method annotations, but the eight
InlineQueryResultCached*classes still inherit onlyInlineQueryResultCachedBase. Consequently valid cached results are notInlineQueryResultinstances and are rejected by static type checkers for methods such asanswer_web_app_query. Make the cached base derive from this common public base, or define the public annotation as a union covering both hierarchies.
class InlineQueryResult(ABC, Dictionaryable, JsonSerializable):
1. MessageId inherit MessageID to fix compatibility issues mentioned by CoPilot. 2. InlineQueryResult inherit InlineQueryResultBase to fix compatibility issues mentioned by CoPilot. 3. InlineQueryResult inherit InlineQueryResultCachedBase inherit InlineQueryResultBase to remove InlineQueryResultBase - no such class in API, all InlineQueryResultCachedxxx are based on InlineQueryResult 4. description and show_caption_above_media added to InlineQueryResult to short inherited code. 5. InlineQueryResultCachedxxx equipped with to_dict() as far as they are Dictionaryable. I don't know how they work before - specific fields were passed to payload_dic but never used... 6. deprecated properties like "thumb_url" were removed: I was wrong when added them: these classes are not JsonDeserializable, so no one should READ fields...
|
Last commit - big base classes revision.
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Suppressed comments (3)
telebot/types.py:4833
- This inheritance preserves nominal compatibility by making every result—including
InlineQueryResultArticle—anInlineQueryResultCachedBase, while the deprecated bases no longer retain their former constructor/serialization behavior. Runtime dispatch using the cached base becomes incorrect, and existing custom subclasses that call the old base constructors break. MakeInlineQueryResultthe common implementation and retain the two legacy bases as compatibility wrappers on their appropriate non-cached/cached branches.
class InlineQueryResult(InlineQueryResultCachedBase, ABC):
telebot/types.py:4936
- The documented type now contradicts the unchanged constructor:
hide_urlis stillOptional[bool], not a string. Keep this documented asbooleven though the parameter is deprecated.
:type hide_url: :obj:`str`
telebot/types.py:6251
titleis not a parameter or local variable inInlineQueryResultCachedAudio.__init__, so every construction of this result raisesNameErrorbefore settingaudio_file_id. Remove this keyword from the base call; cached audio results do not define a title.
super().__init__('audio', id, title = title, caption = caption,
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.
Suppressed comments (4)
telebot/types.py:4895
- The unified serializer and migrations of all cached result classes are not exercised by the test suite; the only test change covers
MessageId. Add representative cached and non-cached serialization tests, including subtype compatibility and optional fields such asdescriptionandshow_caption_above_media=False, so regressions in the new shared path are detected.
def to_json(self):
return json.dumps(self.to_dict())
def to_dict(self):
telebot/types.py:965
MessageIdis independent of the deprecated compatibility class, so values now returned bycopy_message(s)/forward_messagesno longer satisfy existingisinstance(value, MessageID)checks. Make the canonical class inherit the compatibility class; its own__init__already avoids emitting the deprecation warning for new-name users.
class MessageId(JsonDeserializable, ABC):
telebot/types.py:4842
- This inheritance makes every result—including non-cached results such as
InlineQueryResultArticle—anInlineQueryResultCachedBase. That reverses the previous runtime distinction and can send existingisinstance(..., InlineQueryResultCachedBase)dispatch down the cached path. DefineInlineQueryResultfromInlineQueryResultBase, then makeInlineQueryResultCachedBaseinherit the implemented class and use it only for the cached subclasses.
class InlineQueryResult(InlineQueryResultCachedBase, ABC):
telebot/types.py:4968
- After removing
hide_urlfrom subclass serialization, the compatibility branch setsurlto'', butto_dict()still uses a truthiness check and therefore omitsurlentirely. This also contradicts the new guidance to pass an empty URL. Serialize any explicitly supplied URL, including the empty string.
super().__init__('article', id, title = title, input_message_content = input_message_content,
reply_markup = reply_markup, description = description)
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.
Suppressed comments (2)
telebot/types.py:4842
- Making
InlineQueryResultinheritInlineQueryResultCachedBasemeans every non-cached result, such asInlineQueryResultArticleand URL-basedInlineQueryResultPhoto, now passesisinstance(result, InlineQueryResultCachedBase). This breaks the cached/non-cached distinction exposed by the legacy base. MakeInlineQueryResultderive from the general compatibility base, then derive the cached compatibility base from it and use that base only for theInlineQueryResultCached*classes.
class InlineQueryResult(InlineQueryResultCachedBase, ABC):
telebot/types.py:4832
InlineQueryResultBaseis retained and documented as deprecated, but it no longer provides the initializer or serialization methods that it provided before this change. Existing user-defined subclasses that still inherit this compatibility type will now fall through toDictionaryable.to_dict()/JsonSerializable.to_json()and raiseNotImplementedError. Keep forwarding implementations on the deprecated base (or make the old name an alias of the functional implementation) so retaining the name actually preserves existing subclasses.
class InlineQueryResultBase(Dictionaryable, JsonSerializable, ABC):
"""
Deprecated. Use `InlineQueryResult` instead.
"""
|
Let's stop on this now. @coder2020official Sorry for so much noise yesterday/today. I had no plans to revise types, but life brought me here 😂 I'll be AFK next days so hope will not disturb you for some time ) |
|
I can take some time reviewing this |
|
No problem, I understand. Just do not forget ) I currently work on automation of validation of classes (types.py). And hope to get with it the automation of generation updates for bot API changes there. |
|
Just to be clear. The update in this PR is a first step to automation: it aligns classes naming with API. Of course I can bypass these differences, but I propose to align them. |
|
You mean you're trying to auto-generate every API update? Sounds great |
Wrong parameter name was used...
|
lgtm |
Description
MessageID left as deprecated for compatibility.
MaybeInaccessibleMessage added obviously as Union to conform API.