From 3ec7bb819ca7a316e4f02fd4f4e6b1759eaefb7a Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:13:43 -0400 Subject: [PATCH] Make `ChatAdministratorRights.can_*_stories` Required (API 7.1) (#4192) Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> --- telegram/_chatadministratorrights.py | 17 +++++------ telegram/_chatmember.py | 17 +++++------ tests/test_chatadministratorrights.py | 42 ++++----------------------- tests/test_chatmember.py | 22 ++------------ tests/test_chatmemberupdated.py | 8 ++--- tests/test_official/exceptions.py | 9 +----- 6 files changed, 29 insertions(+), 86 deletions(-) diff --git a/telegram/_chatadministratorrights.py b/telegram/_chatadministratorrights.py index fd6e45596..5fdfe8f7c 100644 --- a/telegram/_chatadministratorrights.py +++ b/telegram/_chatadministratorrights.py @@ -44,6 +44,11 @@ class ChatAdministratorRights(TelegramObject): :attr:`can_post_stories`, :attr:`can_edit_stories`, and :attr:`can_delete_stories` are considered as well when comparing objects of this type in terms of equality. + .. versionchanged:: NEXT.VERSION + As of this version, :attr:`can_post_stories`, :attr:`can_edit_stories`, + and :attr:`can_delete_stories` is now required. Thus, the order of arguments had to be + changed. + Args: is_anonymous (:obj:`bool`): :obj:`True`, if the user's presence in the chat is hidden. can_manage_chat (:obj:`bool`): :obj:`True`, if the administrator can access the chat event @@ -169,13 +174,13 @@ class ChatAdministratorRights(TelegramObject): can_promote_members: bool, can_change_info: bool, can_invite_users: bool, + can_post_stories: bool, + can_edit_stories: bool, + can_delete_stories: bool, can_post_messages: Optional[bool] = None, can_edit_messages: Optional[bool] = None, can_pin_messages: Optional[bool] = None, can_manage_topics: Optional[bool] = None, - can_post_stories: Optional[bool] = None, - can_edit_stories: Optional[bool] = None, - can_delete_stories: Optional[bool] = None, *, api_kwargs: Optional[JSONDict] = None, ) -> None: @@ -189,12 +194,6 @@ class ChatAdministratorRights(TelegramObject): self.can_promote_members: bool = can_promote_members self.can_change_info: bool = can_change_info self.can_invite_users: bool = can_invite_users - # Not actually optionals but because of backwards compatability we pretend they are - if can_post_stories is None or can_edit_stories is None or can_delete_stories is None: - raise TypeError( - "As of v21.0 can_post_stories, can_edit_stories and can_delete_stories" - " must be set in order to create this object." - ) self.can_post_stories: bool = can_post_stories self.can_edit_stories: bool = can_edit_stories self.can_delete_stories: bool = can_delete_stories diff --git a/telegram/_chatmember.py b/telegram/_chatmember.py index b42866b3a..920ca8e40 100644 --- a/telegram/_chatmember.py +++ b/telegram/_chatmember.py @@ -191,6 +191,11 @@ class ChatMemberAdministrator(ChatMember): * The argument :paramref:`can_manage_topics` was added, which changes the position of the optional argument :paramref:`custom_title`. + .. versionchanged:: NEXT.VERSION + As of this version, :attr:`can_post_stories`, :attr:`can_edit_stories`, + and :attr:`can_delete_stories` is now required. Thus, the order of arguments had to be + changed. + Args: user (:class:`telegram.User`): Information about the user. can_be_edited (:obj:`bool`): :obj:`True`, if the bot @@ -340,14 +345,14 @@ class ChatMemberAdministrator(ChatMember): can_promote_members: bool, can_change_info: bool, can_invite_users: bool, + can_post_stories: bool, + can_edit_stories: bool, + can_delete_stories: bool, can_post_messages: Optional[bool] = None, can_edit_messages: Optional[bool] = None, can_pin_messages: Optional[bool] = None, can_manage_topics: Optional[bool] = None, custom_title: Optional[str] = None, - can_post_stories: Optional[bool] = None, - can_edit_stories: Optional[bool] = None, - can_delete_stories: Optional[bool] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -362,12 +367,6 @@ class ChatMemberAdministrator(ChatMember): self.can_promote_members: bool = can_promote_members self.can_change_info: bool = can_change_info self.can_invite_users: bool = can_invite_users - # Not actually optionals but because of backwards compatability we pretend they are - if can_post_stories is None or can_edit_stories is None or can_delete_stories is None: - raise TypeError( - "As of 21.0 can_post_stories, can_edit_stories and can_delete_stories " - "must be set in order to create this object." - ) self.can_post_stories: bool = can_post_stories self.can_edit_stories: bool = can_edit_stories self.can_delete_stories: bool = can_delete_stories diff --git a/tests/test_chatadministratorrights.py b/tests/test_chatadministratorrights.py index c7b30be92..e630693c2 100644 --- a/tests/test_chatadministratorrights.py +++ b/tests/test_chatadministratorrights.py @@ -98,38 +98,23 @@ class TestChatAdministratorRightsWithoutRequest: a = ChatAdministratorRights( True, *((False,) * 11), - can_post_stories=False, - can_edit_stories=False, - can_delete_stories=False, ) b = ChatAdministratorRights( True, *((False,) * 11), - can_post_stories=False, - can_edit_stories=False, - can_delete_stories=False, ) c = ChatAdministratorRights( *(False,) * 12, - can_post_stories=False, - can_edit_stories=False, - can_delete_stories=False, ) d = ChatAdministratorRights( True, True, *((False,) * 10), - can_post_stories=False, - can_edit_stories=False, - can_delete_stories=False, ) e = ChatAdministratorRights( True, True, *((False,) * 10), - can_post_stories=False, - can_edit_stories=False, - can_delete_stories=False, ) assert a == b @@ -156,9 +141,8 @@ class TestChatAdministratorRightsWithoutRequest: True, True, True, - can_post_stories=True, - can_edit_stories=True, - can_delete_stories=True, + True, + True, ) t = ChatAdministratorRights.all_rights() # if the dirs are the same, the attributes will all be there @@ -181,9 +165,9 @@ class TestChatAdministratorRightsWithoutRequest: False, False, False, - can_post_stories=False, - can_edit_stories=False, - can_delete_stories=False, + False, + False, + False, ) t = ChatAdministratorRights.no_rights() # if the dirs are the same, the attributes will all be there @@ -194,19 +178,3 @@ class TestChatAdministratorRightsWithoutRequest: assert t[key] is False # and as a finisher, make sure the default is different. assert f != t - - def test_depreciation_typeerror(self): - with pytest.raises(TypeError, match="must be set in order"): - ChatAdministratorRights( - *(False,) * 12, - ) - with pytest.raises(TypeError, match="must be set in order"): - ChatAdministratorRights(*(False,) * 12, can_edit_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatAdministratorRights(*(False,) * 12, can_post_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatAdministratorRights(*(False,) * 12, can_delete_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatAdministratorRights(*(False,) * 12, can_edit_stories=True, can_post_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatAdministratorRights(*(False,) * 12, can_delete_stories=True, can_post_stories=True) diff --git a/tests/test_chatmember.py b/tests/test_chatmember.py index 90ea90294..28293a6cc 100644 --- a/tests/test_chatmember.py +++ b/tests/test_chatmember.py @@ -89,14 +89,14 @@ def chat_member_administrator(): CMDefaults.can_promote_members, CMDefaults.can_change_info, CMDefaults.can_invite_users, + CMDefaults.can_post_stories, + CMDefaults.can_edit_stories, + CMDefaults.can_delete_stories, CMDefaults.can_post_messages, CMDefaults.can_edit_messages, CMDefaults.can_pin_messages, CMDefaults.can_manage_topics, CMDefaults.custom_title, - CMDefaults.can_post_stories, - CMDefaults.can_edit_stories, - CMDefaults.can_delete_stories, ) @@ -302,19 +302,3 @@ class TestChatMemberTypesWithoutRequest: assert c != e assert hash(c) != hash(e) - - def test_deprecation_typeerror(self, chat_member_type): - with pytest.raises(TypeError, match="must be set in order"): - ChatMemberAdministrator( - *(False,) * 12, - ) - with pytest.raises(TypeError, match="must be set in order"): - ChatMemberAdministrator(*(False,) * 12, can_edit_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatMemberAdministrator(*(False,) * 12, can_post_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatMemberAdministrator(*(False,) * 12, can_delete_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatMemberAdministrator(*(False,) * 12, can_edit_stories=True, can_post_stories=True) - with pytest.raises(TypeError, match="must be set in order"): - ChatMemberAdministrator(*(False,) * 12, can_delete_stories=True, can_post_stories=True) diff --git a/tests/test_chatmemberupdated.py b/tests/test_chatmemberupdated.py index 8f9c405c0..0cf5e5810 100644 --- a/tests/test_chatmemberupdated.py +++ b/tests/test_chatmemberupdated.py @@ -54,7 +54,6 @@ def old_chat_member(user): def new_chat_member(user): return ChatMemberAdministrator( user, - TestChatMemberUpdatedBase.new_status, True, True, True, @@ -64,9 +63,10 @@ def new_chat_member(user): True, True, True, - can_post_stories=True, - can_edit_stories=True, - can_delete_stories=True, + True, + True, + True, + custom_title=TestChatMemberUpdatedBase.new_status, ) diff --git a/tests/test_official/exceptions.py b/tests/test_official/exceptions.py index edb1ed2ab..7807a0278 100644 --- a/tests/test_official/exceptions.py +++ b/tests/test_official/exceptions.py @@ -132,9 +132,6 @@ def ptb_extra_params(object_name: str) -> set[str]: # Mostly due to the value being fixed anyway PTB_IGNORED_PARAMS = { r"InlineQueryResult\w+": {"type"}, - # TODO: Remove this in v21.0 (API 7.1) when this can stop being optional - r"ChatAdministratorRights": {"can_post_stories", "can_edit_stories", "can_delete_stories"}, - r"ChatMemberAdministrator": {"can_post_stories", "can_edit_stories", "can_delete_stories"}, r"ChatMember\w+": {"status"}, r"PassportElementError\w+": {"source"}, "ForceReply": {"force_reply"}, @@ -169,11 +166,7 @@ def ignored_param_requirements(object_name: str) -> set[str]: # Arguments that are optional arguments for now for backwards compatibility -BACKWARDS_COMPAT_KWARGS: dict[str, set[str]] = { - # TODO: Remove this in v21.0 (API 7.1) when this can stop being optional - r"ChatAdministratorRights": {"can_post_stories", "can_edit_stories", "can_delete_stories"}, - r"ChatMemberAdministrator": {"can_post_stories", "can_edit_stories", "can_delete_stories"}, -} +BACKWARDS_COMPAT_KWARGS: dict[str, set[str]] = {} def backwards_compat_kwargs(object_name: str) -> set[str]: