Skip to content

Commit

Permalink
refactor: helper/views/messages: Use a dataclass instead of a TypedDict.
Browse files Browse the repository at this point in the history
If the dataclass is chosen, we could squash this commit.
If the TypeDict is chosen, we could drop this commit.
  • Loading branch information
Niloth-p committed Jul 22, 2024
1 parent 7235df2 commit 224ee15
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
26 changes: 9 additions & 17 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@

@pytest.fixture
def msg_info_content() -> MessageInfoPopupContent:
return MessageInfoPopupContent(
message=Message(),
topic_links=OrderedDict(),
message_links=OrderedDict(),
time_mentions=list(),
)
return MessageInfoPopupContent(Message(), OrderedDict(), OrderedDict(), list())


class TestPopUpConfirmationView:
Expand Down Expand Up @@ -519,7 +514,7 @@ def mock_external_classes(
# the message data currently, message_fixture is not used to avoid
# adding extra test runs unnecessarily.
self.msg_info_content = msg_info_content
self.msg_info_content["message"] = Message(id=1)
self.msg_info_content.message = Message(id=1)
self.full_rendered_message = FullRenderedMsgView(
controller=self.controller,
title="Full Rendered Message",
Expand Down Expand Up @@ -593,7 +588,7 @@ def mock_external_classes(
# the message data currently, message_fixture is not used to avoid
# adding extra test runs unnecessarily.
self.msg_info_content = msg_info_content
self.msg_info_content["message"] = Message(id=1)
self.msg_info_content.message = Message(id=1)
self.full_raw_message = FullRawMsgView(
controller=self.controller,
title="Full Raw Message",
Expand Down Expand Up @@ -660,7 +655,7 @@ def mock_external_classes(
# the message data currently, message_fixture is not used to avoid
# adding extra test runs unnecessarily.
self.msg_info_content = msg_info_content
self.msg_info_content["message"] = Message(id=1)
self.msg_info_content.message = Message(id=1)
self.edit_history_view = EditHistoryView(
controller=self.controller,
title="Edit History",
Expand All @@ -672,7 +667,7 @@ def test_init(self) -> None:
assert self.edit_history_view.title == "Edit History"
assert self.edit_history_view.msg_info_content == self.msg_info_content
self.controller.model.fetch_message_history.assert_called_once_with(
message_id=self.msg_info_content["message"]["id"],
message_id=self.msg_info_content.message["id"],
)

@pytest.mark.parametrize("key", keys_for_command("MSG_INFO"))
Expand Down Expand Up @@ -979,7 +974,7 @@ def mock_external_classes(
"Tue Mar 13 10:55:37",
]
self.msg_info_content = msg_info_content
self.msg_info_content["message"] = message_fixture
self.msg_info_content.message = message_fixture
self.msg_info_view = MsgInfoView(
self.controller,
"Message Information",
Expand All @@ -993,10 +988,7 @@ def test_pop_up_info_order(self, message_fixture: Message) -> None:
topic_links = OrderedDict([("https://bar.com", ("topic", 1, True))])
message_links = OrderedDict([("image.jpg", ("image", 1, True))])
msg_info_content = MessageInfoPopupContent(
message=message_fixture,
topic_links=topic_links,
message_links=message_links,
time_mentions=list(),
message_fixture, topic_links, message_links, list()
)
msg_info_view = MsgInfoView(
self.controller,
Expand Down Expand Up @@ -1190,8 +1182,8 @@ def test_height_reactions(
to_vary_in_each_message: Message,
msg_info_content: MessageInfoPopupContent,
) -> None:
msg_info_content["message"] = message_fixture
msg_info_content["message"].update(to_vary_in_each_message)
msg_info_content.message = message_fixture
msg_info_content.message.update(to_vary_in_each_message)
self.msg_info_view = MsgInfoView(
self.controller, "Message Information", msg_info_content
)
Expand Down
4 changes: 3 additions & 1 deletion zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
from collections import defaultdict
from contextlib import contextmanager
from dataclasses import dataclass
from functools import partial, wraps
from itertools import chain, combinations
from re import ASCII, MULTILINE, findall, match
Expand Down Expand Up @@ -57,7 +58,8 @@ class StreamData(TypedDict):
description: str


class MessageInfoPopupContent(TypedDict):
@dataclass
class MessageInfoPopupContent:
message: Message
topic_links: Dict[str, Tuple[str, int, bool]]
message_links: Dict[str, Tuple[str, int, bool]]
Expand Down
8 changes: 4 additions & 4 deletions zulipterminal/ui_tools/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,10 +1122,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
elif is_command_key("MSG_INFO", key):
self.model.controller.show_msg_info(
MessageInfoPopupContent(
message=self.message,
topic_links=self.topic_links,
message_links=self.message_links,
time_mentions=self.time_mentions,
self.message,
self.topic_links,
self.message_links,
self.time_mentions,
)
)
elif is_command_key("ADD_REACTION", key):
Expand Down
19 changes: 8 additions & 11 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,10 +1578,10 @@ def __init__(
msg_info_content: MessageInfoPopupContent,
) -> None:
self.msg_info_content = msg_info_content
self.msg = msg_info_content["message"]
self.topic_links = msg_info_content["topic_links"]
self.message_links = msg_info_content["message_links"]
self.time_mentions = msg_info_content["time_mentions"]
self.msg = msg_info_content.message
self.topic_links = msg_info_content.topic_links
self.message_links = msg_info_content.message_links
self.time_mentions = msg_info_content.time_mentions
self.server_url = controller.model.server_url
date_and_time = controller.model.formatted_local_time(
self.msg["timestamp"], show_seconds=True, show_year=True
Expand Down Expand Up @@ -1721,10 +1721,7 @@ def create_link_buttons(

def keypress(self, size: urwid_Size, key: str) -> str:
msg_info_content = MessageInfoPopupContent(
message=self.msg,
topic_links=self.topic_links,
message_links=self.message_links,
time_mentions=self.time_mentions,
self.msg, self.topic_links, self.message_links, self.time_mentions
)
if is_command_key("EDIT_HISTORY", key) and self.show_edit_history_label:
self.controller.show_edit_history(msg_info_content)
Expand Down Expand Up @@ -1789,7 +1786,7 @@ def __init__(
) -> None:
self.controller = controller
self.msg_info_content = msg_info_content
self.message = msg_info_content["message"]
self.message = msg_info_content.message
width = 64
widgets: List[Any] = []

Expand Down Expand Up @@ -1897,7 +1894,7 @@ def __init__(
) -> None:
self.controller = controller
self.msg_info_content = msg_info_content
self.message = msg_info_content["message"]
self.message = msg_info_content.message
max_cols, max_rows = controller.maximum_popup_dimensions()

# Get rendered message
Expand Down Expand Up @@ -1931,7 +1928,7 @@ def __init__(
) -> None:
self.controller = controller
self.msg_info_content = msg_info_content
self.message = msg_info_content["message"]
self.message = msg_info_content.message
max_cols, max_rows = controller.maximum_popup_dimensions()

# Get rendered message header and footer
Expand Down

0 comments on commit 224ee15

Please sign in to comment.