From 98e8c4e1906c04efe43f6823dcd0784330c1adf6 Mon Sep 17 00:00:00 2001 From: poolitzer Date: Sun, 10 Apr 2022 16:47:16 +0200 Subject: [PATCH 1/9] Feat: To_true shortcut --- telegram/_bot.py | 3 ++- telegram/_chatpermissions.py | 16 ++++++++++++++++ tests/test_chatpermissions.py | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 08ebadf2757..c8df5e62253 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -4802,7 +4802,8 @@ async def restrict_chat_member( 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. + restrictions from a user, see + :meth:`telegram.ChatPermissions.all_true` for a shortcut. 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 c7b076c5118..171463867e4 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -121,3 +121,19 @@ def __init__( self.can_invite_users, self.can_pin_messages, ) + + @staticmethod + def all_true() -> 'ChatPermissions': + """ + This method returns the ChatPermissions object with all attributes set to :obj:`bool`. + This is e.g. useful when unrestricting a ChatMember with + :meth:`telegram.Bot.restrict_chat_member`. + """ + # we generate the object so we can set the attributes to True dynamically in case + # there are more added later. + object_to_return = ChatPermissions() + # slots return us all attributes as a set of strings, + # which is exactly what we need to set them + for attribute in object_to_return.__slots__: + setattr(object_to_return, attribute, True) + return object_to_return diff --git a/tests/test_chatpermissions.py b/tests/test_chatpermissions.py index 353c752b718..1cb14de86b2 100644 --- a/tests/test_chatpermissions.py +++ b/tests/test_chatpermissions.py @@ -124,3 +124,13 @@ def test_equality(self): assert a != d assert hash(a) != hash(d) + + def test_all_true(self): + f = ChatPermissions() + t = ChatPermissions.all_true() + # 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. to_dict gives them to us + # as a dict, mapping the attribute name to the value. + for key in t.to_dict().keys(): + assert t[key] is True From e8cc6d7e855ce4ba5732c6650de24f576a80eba9 Mon Sep 17 00:00:00 2001 From: poolitzer Date: Sun, 10 Apr 2022 16:51:48 +0200 Subject: [PATCH 2/9] Feat: add versionadded directive --- telegram/_chatpermissions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index 171463867e4..3e80c2558c8 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -128,6 +128,8 @@ def all_true() -> 'ChatPermissions': This method returns the ChatPermissions object with all attributes set to :obj:`bool`. This is e.g. useful when unrestricting a ChatMember with :meth:`telegram.Bot.restrict_chat_member`. + + .. versionadded:: 14.0 """ # we generate the object so we can set the attributes to True dynamically in case # there are more added later. From 071754debefcce899b8adc7a9df3be242d82b6e4 Mon Sep 17 00:00:00 2001 From: poolitzer Date: Sun, 10 Apr 2022 19:19:35 +0200 Subject: [PATCH 3/9] Fix: Move static to class method Also now the _id_attrs are updated. --- telegram/_chatpermissions.py | 13 +++---------- tests/test_chatpermissions.py | 10 ++++++---- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index 3e80c2558c8..a5c4b7104d6 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -122,8 +122,8 @@ def __init__( self.can_pin_messages, ) - @staticmethod - def all_true() -> 'ChatPermissions': + @classmethod + def all_true(cls) -> 'ChatPermissions': """ This method returns the ChatPermissions object with all attributes set to :obj:`bool`. This is e.g. useful when unrestricting a ChatMember with @@ -131,11 +131,4 @@ def all_true() -> 'ChatPermissions': .. versionadded:: 14.0 """ - # we generate the object so we can set the attributes to True dynamically in case - # there are more added later. - object_to_return = ChatPermissions() - # slots return us all attributes as a set of strings, - # which is exactly what we need to set them - for attribute in object_to_return.__slots__: - setattr(object_to_return, attribute, True) - return object_to_return + return cls(True, True, True, True, True, True, True, True) diff --git a/tests/test_chatpermissions.py b/tests/test_chatpermissions.py index 1cb14de86b2..c682283cb46 100644 --- a/tests/test_chatpermissions.py +++ b/tests/test_chatpermissions.py @@ -130,7 +130,9 @@ def test_all_true(self): t = ChatPermissions.all_true() # 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. to_dict gives them to us - # as a dict, mapping the attribute name to the value. - for key in t.to_dict().keys(): - assert t[key] is True + # 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._id_attrs: + assert key is True + # and as a finisher, make sure the default is different. + assert f != t From 355378aab98ee65f9e6a35cdaf4465873b85b98d Mon Sep 17 00:00:00 2001 From: poolitzer Date: Sun, 10 Apr 2022 19:59:53 +0200 Subject: [PATCH 4/9] Feat: adding no_permissions also renamed all_true to all_permissions. And fixed the docstring of it --- telegram/_chatpermissions.py | 13 +++++++++++-- tests/test_chatpermissions.py | 16 ++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index a5c4b7104d6..ac8dadfa65e 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -123,12 +123,21 @@ def __init__( ) @classmethod - def all_true(cls) -> 'ChatPermissions': + def all_permissions(cls) -> 'ChatPermissions': """ - This method returns the ChatPermissions object with all attributes set to :obj:`bool`. + This method returns the ChatPermissions object with all attributes set to :obj:`True`. This is e.g. useful when unrestricting a ChatMember with :meth:`telegram.Bot.restrict_chat_member`. .. versionadded:: 14.0 """ return cls(True, True, True, True, True, True, True, True) + + @classmethod + def no_permissions(cls) -> 'ChatPermissions': + """ + This method returns the ChatPermissions object with all attributes set to :obj:`False`. + + .. versionadded:: 14.0 + """ + return cls(False, False, False, False, False, False, False, False) diff --git a/tests/test_chatpermissions.py b/tests/test_chatpermissions.py index c682283cb46..fb7b684e441 100644 --- a/tests/test_chatpermissions.py +++ b/tests/test_chatpermissions.py @@ -125,9 +125,9 @@ def test_equality(self): assert a != d assert hash(a) != hash(d) - def test_all_true(self): + def test_all_permissions(self): f = ChatPermissions() - t = ChatPermissions.all_true() + 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, @@ -136,3 +136,15 @@ def test_all_true(self): assert 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._id_attrs: + assert key is False + # and as a finisher, make sure the default is different. + assert f != t From 15ec7dc95f493ec38f5c683ef3c2955d16cac242 Mon Sep 17 00:00:00 2001 From: poolitzer Date: Sun, 10 Apr 2022 20:04:10 +0200 Subject: [PATCH 5/9] Fix: Use slots in tests --- tests/test_chatpermissions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_chatpermissions.py b/tests/test_chatpermissions.py index fb7b684e441..74048cb4fda 100644 --- a/tests/test_chatpermissions.py +++ b/tests/test_chatpermissions.py @@ -132,8 +132,8 @@ def test_all_permissions(self): 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._id_attrs: - assert key is True + for key in t.__slots__: + assert t[key] is True # and as a finisher, make sure the default is different. assert f != t @@ -144,7 +144,7 @@ def test_no_permissions(self): 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._id_attrs: - assert key is False + for key in t.__slots__: + assert t[key] is False # and as a finisher, make sure the default is different. assert f != t From 2d741ae639089cd55c892ae2e55be4a4c0bb1f6f Mon Sep 17 00:00:00 2001 From: poolitzer Date: Wed, 27 Apr 2022 10:19:23 +0200 Subject: [PATCH 6/9] Fix: Better wording of docs --- telegram/_bot.py | 14 +++++++------- telegram/_chatpermissions.py | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index c8df5e62253..f01730e9713 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -4801,9 +4801,9 @@ async def restrict_chat_member( """ 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, see - :meth:`telegram.ChatPermissions.all_true` for a shortcut. + :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 @@ -4832,11 +4832,11 @@ async def restrict_chat_member( api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the Telegram API. - Returns: - :obj:`bool`: On success, :obj:`True` is returned. + Returns: + :obj:`bool`: On success, :obj:`True` is returned. - Raises: - :class:`telegram.error.TelegramError` + Raises: + :class:`telegram.error.TelegramError` """ data: JSONDict = { 'chat_id': chat_id, diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index ac8dadfa65e..6421edc6e6d 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -126,7 +126,7 @@ def __init__( def all_permissions(cls) -> 'ChatPermissions': """ This method returns the ChatPermissions object with all attributes set to :obj:`True`. - This is e.g. useful when unrestricting a ChatMember with + This is e.g. useful when unrestricting a chat member with :meth:`telegram.Bot.restrict_chat_member`. .. versionadded:: 14.0 From 3c5a9d34b95b96f93f424cdd49a366b36bb5029e Mon Sep 17 00:00:00 2001 From: poolitzer Date: Wed, 27 Apr 2022 10:32:32 +0200 Subject: [PATCH 7/9] Fix: Even better wording of docs --- telegram/_chatpermissions.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index 6421edc6e6d..7b5d48e56e6 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -125,8 +125,8 @@ def __init__( @classmethod def all_permissions(cls) -> 'ChatPermissions': """ - This method returns the ChatPermissions object with all attributes set to :obj:`True`. - This is e.g. useful when unrestricting a chat member with + This method returns an :class:`ChatPermisisons` 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:: 14.0 @@ -136,7 +136,8 @@ def all_permissions(cls) -> 'ChatPermissions': @classmethod def no_permissions(cls) -> 'ChatPermissions': """ - This method returns the ChatPermissions object with all attributes set to :obj:`False`. + This method returns an :class:`ChatPermisisons` instance + with all attributes set to :obj:`False`. .. versionadded:: 14.0 """ From 6ba873ee812ce1841e1216e6d2f3fba1f6e8679a Mon Sep 17 00:00:00 2001 From: poolitzer Date: Wed, 27 Apr 2022 10:43:45 +0200 Subject: [PATCH 8/9] Fix: Switch V14 to V20 --- telegram/_chatpermissions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index 7b5d48e56e6..a2ba9b3cee3 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -129,7 +129,7 @@ def all_permissions(cls) -> 'ChatPermissions': :obj:`True`. This is e.g. useful when unrestricting a chat member with :meth:`telegram.Bot.restrict_chat_member`. - .. versionadded:: 14.0 + .. versionadded:: 20.0 """ return cls(True, True, True, True, True, True, True, True) @@ -139,6 +139,6 @@ def no_permissions(cls) -> 'ChatPermissions': This method returns an :class:`ChatPermisisons` instance with all attributes set to :obj:`False`. - .. versionadded:: 14.0 + .. versionadded:: 2.0 """ return cls(False, False, False, False, False, False, False, False) From 220aaee83a1d1b8229addb05508c4eb02a69cfb4 Mon Sep 17 00:00:00 2001 From: poolitzer Date: Wed, 27 Apr 2022 11:55:53 +0200 Subject: [PATCH 9/9] Fix: Actually V20, and inner class link --- telegram/_bot.py | 8 ++++---- telegram/_chatpermissions.py | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index f01730e9713..b25f9329146 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -4832,11 +4832,11 @@ async def restrict_chat_member( api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to be passed to the Telegram API. - Returns: - :obj:`bool`: On success, :obj:`True` is returned. + Returns: + :obj:`bool`: On success, :obj:`True` is returned. - Raises: - :class:`telegram.error.TelegramError` + Raises: + :class:`telegram.error.TelegramError` """ data: JSONDict = { 'chat_id': chat_id, diff --git a/telegram/_chatpermissions.py b/telegram/_chatpermissions.py index a2ba9b3cee3..308f615d5df 100644 --- a/telegram/_chatpermissions.py +++ b/telegram/_chatpermissions.py @@ -125,20 +125,21 @@ def __init__( @classmethod def all_permissions(cls) -> 'ChatPermissions': """ - This method returns an :class:`ChatPermisisons` instance with all attributes set to - :obj:`True`. This is e.g. useful when unrestricting a chat member with + 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:`ChatPermisisons` instance + This method returns an :class:`ChatPermissions` instance with all attributes set to :obj:`False`. - .. versionadded:: 2.0 + .. versionadded:: 20.0 """ return cls(False, False, False, False, False, False, False, False)