Skip to content

Commit a47b244

Browse files
committed
helper: refactor: Use TypedDict for Message.
Note the use of Totality here because stream messages/pms/huddles do not have the same attributes. An alternate declaration of `messages` in initial_index is necessary due to a mypy bug - python/mypy#7217 - while using TypeDict with defaultdict.
1 parent 49d3fa5 commit a47b244

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

zulipterminal/helper.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@
2121
LINUX = platform.system() == "Linux"
2222
WSL = 'microsoft' in platform.release().lower()
2323

24-
Message = Dict[str, Any]
24+
Message = TypedDict('Message', {
25+
'id': int,
26+
'sender_id': int,
27+
'content': str,
28+
'recipient_id': int,
29+
'timestamp': int,
30+
'client': str,
31+
'subject': str,
32+
'topic_links': List[str],
33+
'is_me_message': bool,
34+
'reactions': List[Dict[str, Any]],
35+
'submessages': List[Dict[str, Any]],
36+
'flags': List[str],
37+
'sender_full_name': str,
38+
'sender_short_name': str,
39+
'sender_email': str,
40+
'sender_realm_str': str,
41+
'display_recipient': Any,
42+
'type': str,
43+
'stream_id': int,
44+
'avatar_url': str,
45+
'content_type': str,
46+
}, total=False)
2547

2648
Index = TypedDict('Index', {
2749
'pointer': Dict[str, Union[int, Set[None]]], # narrow_str, message_id
@@ -53,7 +75,8 @@
5375
edited_messages=set(),
5476
topics=defaultdict(list),
5577
search=set(),
56-
messages=defaultdict(dict),
78+
# mypy bug: https://github.com/python/mypy/issues/7217
79+
messages=defaultdict(lambda: Message()),
5780
)
5881

5982

@@ -99,7 +122,7 @@ def _set_count_in_model(new_count: int, changed_messages: List[Message],
99122
# self-pm has only one display_recipient
100123
# 1-1 pms have 2 display_recipient
101124
elif len(message['display_recipient']) <= 2:
102-
key = message['sender_id']
125+
key = message['sender_id'] # type: ignore
103126
unreads = unread_counts['unread_pms'] # type: ignore
104127
else: # If it's a group pm
105128
key = frozenset( # type: ignore

0 commit comments

Comments
 (0)