From 37aac332a982a64d17cddbf85a968e8e8e452495 Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Mon, 25 Oct 2021 21:16:36 +0530 Subject: [PATCH 1/2] Remove redundant setters --- telegram/ext/_callbackcontext.py | 19 -------- telegram/ext/_conversationhandler.py | 43 ------------------ telegram/ext/_defaults.py | 65 +--------------------------- tests/test_conversationhandler.py | 2 +- 4 files changed, 2 insertions(+), 127 deletions(-) diff --git a/telegram/ext/_callbackcontext.py b/telegram/ext/_callbackcontext.py index e62f1c890c9..d07d87a3585 100644 --- a/telegram/ext/_callbackcontext.py +++ b/telegram/ext/_callbackcontext.py @@ -24,7 +24,6 @@ Dict, List, Match, - NoReturn, Optional, Tuple, Union, @@ -153,12 +152,6 @@ def bot_data(self) -> BD: """ return self.dispatcher.bot_data - @bot_data.setter - def bot_data(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to bot_data, see https://git.io/Jt6ic" - ) - @property def chat_data(self) -> Optional[CD]: """:obj:`dict`: Optional. A dict that can be used to keep any data in. For each @@ -174,12 +167,6 @@ def chat_data(self) -> Optional[CD]: return self._chat_id_and_data[1] return None - @chat_data.setter - def chat_data(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to chat_data, see https://git.io/Jt6ic" - ) - @property def user_data(self) -> Optional[UD]: """:obj:`dict`: Optional. A dict that can be used to keep any data in. For each @@ -189,12 +176,6 @@ def user_data(self) -> Optional[UD]: return self._user_id_and_data[1] return None - @user_data.setter - def user_data(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to user_data, see https://git.io/Jt6ic" - ) - def refresh_data(self) -> None: """If :attr:`dispatcher` uses persistence, calls :meth:`telegram.ext.BasePersistence.refresh_bot_data` on :attr:`bot_data`, diff --git a/telegram/ext/_conversationhandler.py b/telegram/ext/_conversationhandler.py index b0b61588e57..4f4cb0a7dc3 100644 --- a/telegram/ext/_conversationhandler.py +++ b/telegram/ext/_conversationhandler.py @@ -27,7 +27,6 @@ TYPE_CHECKING, Dict, List, - NoReturn, Optional, Union, Tuple, @@ -335,10 +334,6 @@ def entry_points(self) -> List[Handler]: """ return self._entry_points - @entry_points.setter - def entry_points(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to entry_points after initialization.') - @property def states(self) -> Dict[object, List[Handler]]: """Dict[:obj:`object`, List[:class:`telegram.ext.Handler`]]: A :obj:`dict` that @@ -347,10 +342,6 @@ def states(self) -> Dict[object, List[Handler]]: """ return self._states - @states.setter - def states(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to states after initialization.') - @property def fallbacks(self) -> List[Handler]: """List[:class:`telegram.ext.Handler`]: A list of handlers that might be used if @@ -359,46 +350,26 @@ def fallbacks(self) -> List[Handler]: """ return self._fallbacks - @fallbacks.setter - def fallbacks(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to fallbacks after initialization.') - @property def allow_reentry(self) -> bool: """:obj:`bool`: Determines if a user can restart a conversation with an entry point.""" return self._allow_reentry - @allow_reentry.setter - def allow_reentry(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to allow_reentry after initialization.') - @property def per_user(self) -> bool: """:obj:`bool`: If the conversation key should contain the User's ID.""" return self._per_user - @per_user.setter - def per_user(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to per_user after initialization.') - @property def per_chat(self) -> bool: """:obj:`bool`: If the conversation key should contain the Chat's ID.""" return self._per_chat - @per_chat.setter - def per_chat(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to per_chat after initialization.') - @property def per_message(self) -> bool: """:obj:`bool`: If the conversation key should contain the message's ID.""" return self._per_message - @per_message.setter - def per_message(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to per_message after initialization.') - @property def conversation_timeout( self, @@ -409,21 +380,11 @@ def conversation_timeout( """ return self._conversation_timeout - @conversation_timeout.setter - def conversation_timeout(self, value: object) -> NoReturn: - raise ValueError( - 'You can not assign a new value to conversation_timeout after initialization.' - ) - @property def name(self) -> Optional[str]: """:obj:`str`: Optional. The name for this :class:`ConversationHandler`.""" return self._name - @name.setter - def name(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to name after initialization.') - @property def map_to_parent(self) -> Optional[Dict[object, object]]: """Dict[:obj:`object`, :obj:`object`]: Optional. A :obj:`dict` that can be @@ -432,10 +393,6 @@ def map_to_parent(self) -> Optional[Dict[object, object]]: """ return self._map_to_parent - @map_to_parent.setter - def map_to_parent(self, value: object) -> NoReturn: - raise ValueError('You can not assign a new value to map_to_parent after initialization.') - @property def persistence(self) -> Optional[BasePersistence]: """The persistence class as provided by the :class:`Dispatcher`.""" diff --git a/telegram/ext/_defaults.py b/telegram/ext/_defaults.py index ac0fbd5cca0..06708e378e9 100644 --- a/telegram/ext/_defaults.py +++ b/telegram/ext/_defaults.py @@ -18,7 +18,7 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. # pylint: disable=no-self-use """This module contains the class Defaults, which allows to pass default values to Updater.""" -from typing import NoReturn, Optional, Dict, Any +from typing import Optional, Dict, Any import pytz @@ -117,13 +117,6 @@ def parse_mode(self) -> Optional[str]: """ return self._parse_mode - @parse_mode.setter - def parse_mode(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def explanation_parse_mode(self) -> Optional[str]: """:obj:`str`: Optional. Alias for :attr:`parse_mode`, used for @@ -131,13 +124,6 @@ def explanation_parse_mode(self) -> Optional[str]: """ return self._parse_mode - @explanation_parse_mode.setter - def explanation_parse_mode(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def disable_notification(self) -> Optional[bool]: """:obj:`bool`: Optional. Sends the message silently. Users will @@ -145,13 +131,6 @@ def disable_notification(self) -> Optional[bool]: """ return self._disable_notification - @disable_notification.setter - def disable_notification(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def disable_web_page_preview(self) -> Optional[bool]: """:obj:`bool`: Optional. Disables link previews for links in this @@ -159,13 +138,6 @@ def disable_web_page_preview(self) -> Optional[bool]: """ return self._disable_web_page_preview - @disable_web_page_preview.setter - def disable_web_page_preview(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def allow_sending_without_reply(self) -> Optional[bool]: """:obj:`bool`: Optional. Pass :obj:`True`, if the message @@ -173,13 +145,6 @@ def allow_sending_without_reply(self) -> Optional[bool]: """ return self._allow_sending_without_reply - @allow_sending_without_reply.setter - def allow_sending_without_reply(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def timeout(self) -> ODVInput[float]: """:obj:`int` | :obj:`float`: Optional. If this value is specified, use it as the @@ -188,13 +153,6 @@ def timeout(self) -> ODVInput[float]: """ return self._timeout - @timeout.setter - def timeout(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def quote(self) -> Optional[bool]: """:obj:`bool`: Optional. If set to :obj:`True`, the reply is sent as an actual reply @@ -203,13 +161,6 @@ def quote(self) -> Optional[bool]: """ return self._quote - @quote.setter - def quote(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def tzinfo(self) -> pytz.BaseTzInfo: """:obj:`tzinfo`: A timezone to be used for all date(time) objects appearing @@ -217,13 +168,6 @@ def tzinfo(self) -> pytz.BaseTzInfo: """ return self._tzinfo - @tzinfo.setter - def tzinfo(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - @property def run_async(self) -> bool: """:obj:`bool`: Optional. Default setting for the ``run_async`` parameter of @@ -232,13 +176,6 @@ def run_async(self) -> bool: """ return self._run_async - @run_async.setter - def run_async(self, value: object) -> NoReturn: - raise AttributeError( - "You can not assign a new value to defaults after because it would " - "not have any effect." - ) - def __hash__(self) -> int: return hash( ( diff --git a/tests/test_conversationhandler.py b/tests/test_conversationhandler.py index 8e69a821c1e..9d1a9dce6c7 100644 --- a/tests/test_conversationhandler.py +++ b/tests/test_conversationhandler.py @@ -282,7 +282,7 @@ def test_immutable(self, attr): assert list(value.keys())[0] == attr else: assert getattr(ch, attr) == attr - with pytest.raises(ValueError, match=f'You can not assign a new value to {attr}'): + with pytest.raises(AttributeError): setattr(ch, attr, True) def test_immutable_per_message(self): From e4cae90e987dc70c3cd0cb9a67b24dcf06b3b5fe Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Mon, 25 Oct 2021 22:10:08 +0530 Subject: [PATCH 2/2] Also remove redundant tests --- tests/test_callbackcontext.py | 14 -------- tests/test_conversationhandler.py | 56 ------------------------------- tests/test_defaults.py | 24 ------------- 3 files changed, 94 deletions(-) diff --git a/tests/test_callbackcontext.py b/tests/test_callbackcontext.py index 0e17fdd30e6..bf6ea1f4e8e 100644 --- a/tests/test_callbackcontext.py +++ b/tests/test_callbackcontext.py @@ -155,20 +155,6 @@ def test_match(self, dp): assert callback_context.match == 'test' - def test_data_assignment(self, dp): - update = Update( - 0, message=Message(0, None, Chat(1, 'chat'), from_user=User(1, 'user', False)) - ) - - callback_context = CallbackContext.from_update(update, dp) - - with pytest.raises(AttributeError): - callback_context.bot_data = {"test": 123} - with pytest.raises(AttributeError): - callback_context.user_data = {} - with pytest.raises(AttributeError): - callback_context.chat_data = "test" - def test_dispatcher_attribute(self, dp): callback_context = CallbackContext(dp) assert callback_context.dispatcher == dp diff --git a/tests/test_conversationhandler.py b/tests/test_conversationhandler.py index 9d1a9dce6c7..cd6debec699 100644 --- a/tests/test_conversationhandler.py +++ b/tests/test_conversationhandler.py @@ -246,62 +246,6 @@ def stop(self, update, context): return self._set_state(update, self.STOPPING) # Tests - @pytest.mark.parametrize( - 'attr', - [ - 'entry_points', - 'states', - 'fallbacks', - 'per_chat', - 'name', - 'per_user', - 'allow_reentry', - 'conversation_timeout', - 'map_to_parent', - ], - indirect=False, - ) - def test_immutable(self, attr): - ch = ConversationHandler( - 'entry_points', - {'states': ['states']}, - 'fallbacks', - per_chat='per_chat', - per_user='per_user', - per_message=False, - allow_reentry='allow_reentry', - conversation_timeout='conversation_timeout', - name='name', - map_to_parent='map_to_parent', - ) - - value = getattr(ch, attr) - if isinstance(value, list): - assert value[0] == attr - elif isinstance(value, dict): - assert list(value.keys())[0] == attr - else: - assert getattr(ch, attr) == attr - with pytest.raises(AttributeError): - setattr(ch, attr, True) - - def test_immutable_per_message(self): - ch = ConversationHandler( - 'entry_points', - {'states': ['states']}, - 'fallbacks', - per_chat='per_chat', - per_user='per_user', - per_message=False, - allow_reentry='allow_reentry', - conversation_timeout='conversation_timeout', - name='name', - map_to_parent='map_to_parent', - ) - assert ch.per_message is False - with pytest.raises(ValueError, match='You can not assign a new value to per_message'): - ch.per_message = True - def test_per_all_false(self): with pytest.raises(ValueError, match="can't all be 'False'"): ConversationHandler( diff --git a/tests/test_defaults.py b/tests/test_defaults.py index ab79c21efea..db19034e366 100644 --- a/tests/test_defaults.py +++ b/tests/test_defaults.py @@ -17,8 +17,6 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. -import pytest - from telegram.ext import Defaults from telegram import User @@ -30,28 +28,6 @@ def test_slot_behaviour(self, mro_slots): assert getattr(a, attr, 'err') != 'err', f"got extra slot '{attr}'" assert len(mro_slots(a)) == len(set(mro_slots(a))), "duplicate slot" - def test_data_assignment(self, dp): - defaults = Defaults() - - with pytest.raises(AttributeError): - defaults.parse_mode = True - with pytest.raises(AttributeError): - defaults.explanation_parse_mode = True - with pytest.raises(AttributeError): - defaults.disable_notification = True - with pytest.raises(AttributeError): - defaults.disable_web_page_preview = True - with pytest.raises(AttributeError): - defaults.allow_sending_without_reply = True - with pytest.raises(AttributeError): - defaults.timeout = True - with pytest.raises(AttributeError): - defaults.quote = True - with pytest.raises(AttributeError): - defaults.tzinfo = True - with pytest.raises(AttributeError): - defaults.run_async = True - def test_equality(self): a = Defaults(parse_mode='HTML', quote=True) b = Defaults(parse_mode='HTML', quote=True)