Skip to content

Commit 9347530

Browse files
committed
helper: 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 9347530

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

zulipterminal/helper.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,31 @@
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, # Only for stream msgs.
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, # Only for stream msgs.
44+
'avatar_url': str,
45+
'content_type': str,
46+
'match_content': str, # If keyword search specified in narrow params.
47+
'match_subject': str, # If keyword search specified in narrow params.
48+
}, total=False)
2549

2650
Index = TypedDict('Index', {
2751
'pointer': Dict[str, Union[int, Set[None]]], # narrow_str, message_id
@@ -53,7 +77,8 @@
5377
edited_messages=set(),
5478
topics=defaultdict(list),
5579
search=set(),
56-
messages=defaultdict(dict),
80+
# mypy bug: https://github.com/python/mypy/issues/7217
81+
messages=defaultdict(lambda: Message()),
5782
)
5883

5984

@@ -99,7 +124,7 @@ def _set_count_in_model(new_count: int, changed_messages: List[Message],
99124
# self-pm has only one display_recipient
100125
# 1-1 pms have 2 display_recipient
101126
elif len(message['display_recipient']) <= 2:
102-
key = message['sender_id']
127+
key = message['sender_id'] # type: ignore
103128
unreads = unread_counts['unread_pms'] # type: ignore
104129
else: # If it's a group pm
105130
key = frozenset( # type: ignore

0 commit comments

Comments
 (0)