Skip to content

Remove redundant setters #2747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions telegram/ext/_callbackcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
Dict,
List,
Match,
NoReturn,
Optional,
Tuple,
Union,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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`,
Expand Down
43 changes: 0 additions & 43 deletions telegram/ext/_conversationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
TYPE_CHECKING,
Dict,
List,
NoReturn,
Optional,
Union,
Tuple,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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`."""
Expand Down
65 changes: 1 addition & 64 deletions telegram/ext/_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -117,69 +117,34 @@ 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
the corresponding parameter of :meth:`telegram.Bot.send_poll`.
"""
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
receive a notification with no sound.
"""
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
message.
"""
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
should be sent even if the specified replied-to message is not found.
"""
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
Expand All @@ -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
Expand All @@ -203,27 +161,13 @@ 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
throughout PTB.
"""
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
Expand All @@ -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(
(
Expand Down
14 changes: 0 additions & 14 deletions tests/test_callbackcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 0 additions & 56 deletions tests/test_conversationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(ValueError, match=f'You can not assign a new value to {attr}'):
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(
Expand Down
Loading