Skip to content

Refactor handling of shortcut-bots #2505

@Bibo-Joshi

Description

@Bibo-Joshi

A bunch of TG-Objects have bot attribute that is used for the shortcuts, e.g. self.bot.send_message(…), which is dangerous in the rare edge cases where users manually instantiate those objects and forget to pass the bot. This will then lead to somewhat unprecise error messages NoneType has no attribute 'send_message'.
Moreover, Message.bot is undocumented, but not private, so people relying on auto-complete may be using it without knowing that it's not really intended for external use.

Proposal:

Do something like

TGObject:
    self._bot: Optional[Bot] = None

    def get_bot(self) -> Bot:
        if self._bot is None:
            raise RuntimeError('This object has no bot and shortcuts cant be used')
        return self._bot

    def set_bot(self, bot: Bot) -> None:
        self._bot = bot

This way we have helpful error messages without having to check if self.bot is None in every shortcut.

An alternate solution would be to make TGObject.bot a property that raises the error if _bot is None. But explicitly calling a method makes it more clear that something could go wrong, IMO.


Note: We briefly discussed this in dev-chat on 04-05-2021, see https://t.me/c/1494805131/15168

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy