From 97281da3516669917e6b6514964126e9f01ec1d2 Mon Sep 17 00:00:00 2001 From: Poolitzer Date: Wed, 27 Apr 2022 20:17:58 +0200 Subject: [PATCH] Add Convenience Shortcuts `ChatPermissions.{all, no}_permissions` (#2948) --- telegram/_bot.py | 5 +++-- telegram/_chatpermissions.py | 22 ++++++++++++++++++++++ tests/test_chatpermissions.py | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 08ebadf27..b25f93291 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -4801,8 +4801,9 @@ class Bot(TelegramObject, AbstractAsyncContextManager): """ Use this method to restrict a user in a supergroup. The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. Pass - :obj:`True` for all boolean parameters in :class:`telegram.ChatPermissions` to lift - restrictions from a user. + :obj:`True` for all boolean parameters to lift restrictions from a user. + + .. seealso:: :meth:`telegram.ChatPermissions.all_permissions` Args: chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index c7b076c51..308f615d5 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -121,3 +121,25 @@ class ChatPermissions(TelegramObject): self.can_invite_users, self.can_pin_messages, ) + + @classmethod + def all_permissions(cls) -> 'ChatPermissions': + """ + This method returns an :class:`ChatPermissions` instance with all attributes + set to :obj:`True`. This is e.g. useful when unrestricting a chat member with + :meth:`telegram.Bot.restrict_chat_member`. + + .. versionadded:: 20.0 + + """ + return cls(True, True, True, True, True, True, True, True) + + @classmethod + def no_permissions(cls) -> 'ChatPermissions': + """ + This method returns an :class:`ChatPermissions` instance + with all attributes set to :obj:`False`. + + .. versionadded:: 20.0 + """ + return cls(False, False, False, False, False, False, False, False) diff --git a/tests/test_chatpermissions.py b/tests/test_chatpermissions.py index 353c752b7..74048cb4f 100644 --- a/tests/test_chatpermissions.py +++ b/tests/test_chatpermissions.py @@ -124,3 +124,27 @@ class TestChatPermissions: assert a != d assert hash(a) != hash(d) + + def test_all_permissions(self): + f = ChatPermissions() + t = ChatPermissions.all_permissions() + # if the dirs are the same, the attributes will all be there + assert dir(f) == dir(t) + # now we just need to check that all attributes are True. _id_attrs returns all values, + # if a new one is added without defaulting to True, this will fail + for key in t.__slots__: + assert t[key] is True + # and as a finisher, make sure the default is different. + assert f != t + + def test_no_permissions(self): + f = ChatPermissions() + t = ChatPermissions.no_permissions() + # if the dirs are the same, the attributes will all be there + assert dir(f) == dir(t) + # now we just need to check that all attributes are True. _id_attrs returns all values, + # if a new one is added without defaulting to True, this will fail + for key in t.__slots__: + assert t[key] is False + # and as a finisher, make sure the default is different. + assert f != t