-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Description
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