Skip to content

Commit 224ee15

Browse files
committed
refactor: helper/views/messages: Use a dataclass instead of a TypedDict.
If the dataclass is chosen, we could squash this commit. If the TypeDict is chosen, we could drop this commit.
1 parent 7235df2 commit 224ee15

File tree

4 files changed

+24
-33
lines changed

4 files changed

+24
-33
lines changed

tests/ui_tools/test_popups.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@
4747

4848
@pytest.fixture
4949
def msg_info_content() -> MessageInfoPopupContent:
50-
return MessageInfoPopupContent(
51-
message=Message(),
52-
topic_links=OrderedDict(),
53-
message_links=OrderedDict(),
54-
time_mentions=list(),
55-
)
50+
return MessageInfoPopupContent(Message(), OrderedDict(), OrderedDict(), list())
5651

5752

5853
class TestPopUpConfirmationView:
@@ -519,7 +514,7 @@ def mock_external_classes(
519514
# the message data currently, message_fixture is not used to avoid
520515
# adding extra test runs unnecessarily.
521516
self.msg_info_content = msg_info_content
522-
self.msg_info_content["message"] = Message(id=1)
517+
self.msg_info_content.message = Message(id=1)
523518
self.full_rendered_message = FullRenderedMsgView(
524519
controller=self.controller,
525520
title="Full Rendered Message",
@@ -593,7 +588,7 @@ def mock_external_classes(
593588
# the message data currently, message_fixture is not used to avoid
594589
# adding extra test runs unnecessarily.
595590
self.msg_info_content = msg_info_content
596-
self.msg_info_content["message"] = Message(id=1)
591+
self.msg_info_content.message = Message(id=1)
597592
self.full_raw_message = FullRawMsgView(
598593
controller=self.controller,
599594
title="Full Raw Message",
@@ -660,7 +655,7 @@ def mock_external_classes(
660655
# the message data currently, message_fixture is not used to avoid
661656
# adding extra test runs unnecessarily.
662657
self.msg_info_content = msg_info_content
663-
self.msg_info_content["message"] = Message(id=1)
658+
self.msg_info_content.message = Message(id=1)
664659
self.edit_history_view = EditHistoryView(
665660
controller=self.controller,
666661
title="Edit History",
@@ -672,7 +667,7 @@ def test_init(self) -> None:
672667
assert self.edit_history_view.title == "Edit History"
673668
assert self.edit_history_view.msg_info_content == self.msg_info_content
674669
self.controller.model.fetch_message_history.assert_called_once_with(
675-
message_id=self.msg_info_content["message"]["id"],
670+
message_id=self.msg_info_content.message["id"],
676671
)
677672

678673
@pytest.mark.parametrize("key", keys_for_command("MSG_INFO"))
@@ -979,7 +974,7 @@ def mock_external_classes(
979974
"Tue Mar 13 10:55:37",
980975
]
981976
self.msg_info_content = msg_info_content
982-
self.msg_info_content["message"] = message_fixture
977+
self.msg_info_content.message = message_fixture
983978
self.msg_info_view = MsgInfoView(
984979
self.controller,
985980
"Message Information",
@@ -993,10 +988,7 @@ def test_pop_up_info_order(self, message_fixture: Message) -> None:
993988
topic_links = OrderedDict([("https://bar.com", ("topic", 1, True))])
994989
message_links = OrderedDict([("image.jpg", ("image", 1, True))])
995990
msg_info_content = MessageInfoPopupContent(
996-
message=message_fixture,
997-
topic_links=topic_links,
998-
message_links=message_links,
999-
time_mentions=list(),
991+
message_fixture, topic_links, message_links, list()
1000992
)
1001993
msg_info_view = MsgInfoView(
1002994
self.controller,
@@ -1190,8 +1182,8 @@ def test_height_reactions(
11901182
to_vary_in_each_message: Message,
11911183
msg_info_content: MessageInfoPopupContent,
11921184
) -> None:
1193-
msg_info_content["message"] = message_fixture
1194-
msg_info_content["message"].update(to_vary_in_each_message)
1185+
msg_info_content.message = message_fixture
1186+
msg_info_content.message.update(to_vary_in_each_message)
11951187
self.msg_info_view = MsgInfoView(
11961188
self.controller, "Message Information", msg_info_content
11971189
)

zulipterminal/helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
from collections import defaultdict
99
from contextlib import contextmanager
10+
from dataclasses import dataclass
1011
from functools import partial, wraps
1112
from itertools import chain, combinations
1213
from re import ASCII, MULTILINE, findall, match
@@ -57,7 +58,8 @@ class StreamData(TypedDict):
5758
description: str
5859

5960

60-
class MessageInfoPopupContent(TypedDict):
61+
@dataclass
62+
class MessageInfoPopupContent:
6163
message: Message
6264
topic_links: Dict[str, Tuple[str, int, bool]]
6365
message_links: Dict[str, Tuple[str, int, bool]]

zulipterminal/ui_tools/messages.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
11221122
elif is_command_key("MSG_INFO", key):
11231123
self.model.controller.show_msg_info(
11241124
MessageInfoPopupContent(
1125-
message=self.message,
1126-
topic_links=self.topic_links,
1127-
message_links=self.message_links,
1128-
time_mentions=self.time_mentions,
1125+
self.message,
1126+
self.topic_links,
1127+
self.message_links,
1128+
self.time_mentions,
11291129
)
11301130
)
11311131
elif is_command_key("ADD_REACTION", key):

zulipterminal/ui_tools/views.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,10 +1578,10 @@ def __init__(
15781578
msg_info_content: MessageInfoPopupContent,
15791579
) -> None:
15801580
self.msg_info_content = msg_info_content
1581-
self.msg = msg_info_content["message"]
1582-
self.topic_links = msg_info_content["topic_links"]
1583-
self.message_links = msg_info_content["message_links"]
1584-
self.time_mentions = msg_info_content["time_mentions"]
1581+
self.msg = msg_info_content.message
1582+
self.topic_links = msg_info_content.topic_links
1583+
self.message_links = msg_info_content.message_links
1584+
self.time_mentions = msg_info_content.time_mentions
15851585
self.server_url = controller.model.server_url
15861586
date_and_time = controller.model.formatted_local_time(
15871587
self.msg["timestamp"], show_seconds=True, show_year=True
@@ -1721,10 +1721,7 @@ def create_link_buttons(
17211721

17221722
def keypress(self, size: urwid_Size, key: str) -> str:
17231723
msg_info_content = MessageInfoPopupContent(
1724-
message=self.msg,
1725-
topic_links=self.topic_links,
1726-
message_links=self.message_links,
1727-
time_mentions=self.time_mentions,
1724+
self.msg, self.topic_links, self.message_links, self.time_mentions
17281725
)
17291726
if is_command_key("EDIT_HISTORY", key) and self.show_edit_history_label:
17301727
self.controller.show_edit_history(msg_info_content)
@@ -1789,7 +1786,7 @@ def __init__(
17891786
) -> None:
17901787
self.controller = controller
17911788
self.msg_info_content = msg_info_content
1792-
self.message = msg_info_content["message"]
1789+
self.message = msg_info_content.message
17931790
width = 64
17941791
widgets: List[Any] = []
17951792

@@ -1897,7 +1894,7 @@ def __init__(
18971894
) -> None:
18981895
self.controller = controller
18991896
self.msg_info_content = msg_info_content
1900-
self.message = msg_info_content["message"]
1897+
self.message = msg_info_content.message
19011898
max_cols, max_rows = controller.maximum_popup_dimensions()
19021899

19031900
# Get rendered message
@@ -1931,7 +1928,7 @@ def __init__(
19311928
) -> None:
19321929
self.controller = controller
19331930
self.msg_info_content = msg_info_content
1934-
self.message = msg_info_content["message"]
1931+
self.message = msg_info_content.message
19351932
max_cols, max_rows = controller.maximum_popup_dimensions()
19361933

19371934
# Get rendered message header and footer

0 commit comments

Comments
 (0)