Skip to content

Commit 1afde76

Browse files
committed
refactor: helper/model/emojis: Use TypedDict for emoji data.
1 parent 27afd7e commit 1afde76

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

zulipterminal/helper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313
)
1414

1515
import lxml.html
16-
from typing_extensions import TypedDict
16+
from typing_extensions import Literal, TypedDict
1717

1818

1919
MACOS = platform.system() == "Darwin"
2020
LINUX = platform.system() == "Linux"
2121
WSL = 'microsoft' in platform.release().lower()
2222

23+
EmojiData = TypedDict('EmojiData', {
24+
'code': str,
25+
'type': Literal['realm_emoji', 'unicode_emoji'],
26+
})
27+
28+
NamedEmojiData = Dict[str, EmojiData]
29+
2330
Message = TypedDict('Message', {
2431
'id': int,
2532
'sender_id': int,

zulipterminal/model.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from concurrent.futures import Future, ThreadPoolExecutor, wait
55
from typing import (
66
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Optional, Set,
7-
Tuple, Union,
7+
Tuple, Union, cast,
88
)
99
from urllib.parse import urlparse
1010

@@ -14,8 +14,9 @@
1414
from zulipterminal import unicode_emojis
1515
from zulipterminal.config.keys import keys_for_command
1616
from zulipterminal.helper import (
17-
Message, asynch, canonicalize_color, classify_unread_counts,
18-
display_error_if_present, index_messages, initial_index, notify, set_count,
17+
Message, NamedEmojiData, asynch, canonicalize_color,
18+
classify_unread_counts, display_error_if_present, index_messages,
19+
initial_index, notify, set_count,
1920
)
2021
from zulipterminal.ui_tools.utils import create_msg_box_list
2122

@@ -139,9 +140,10 @@ def __init__(self, controller: Any) -> None:
139140
unicode_emoji_data = unicode_emojis.EMOJI_DATA
140141
for name, data in unicode_emoji_data.items():
141142
data['type'] = 'unicode_emoji'
143+
typed_unicode_emoji_data = cast(NamedEmojiData, unicode_emoji_data)
142144

143145
custom_emojis = self.fetch_custom_emojis()
144-
all_emojis = (list(unicode_emoji_data.items())
146+
all_emojis = (list(typed_unicode_emoji_data.items())
145147
+ list(custom_emojis.items()))
146148
self.active_emoji_data = OrderedDict(sorted(all_emojis,
147149
key=lambda e: e[0]))
@@ -364,12 +366,12 @@ def update_stream_message(self, topic: str, message_id: int,
364366
display_error_if_present(response, self.controller)
365367
return response['result'] == 'success'
366368

367-
def fetch_custom_emojis(self) -> Dict[str, Dict[str, str]]:
369+
def fetch_custom_emojis(self) -> NamedEmojiData:
368370
response = self.client.get_realm_emoji()
369371
custom_emojis = {emoji['name']: {'code': emoji_code,
370372
'type': 'realm_emoji'}
371373
for emoji_code, emoji in response['emoji'].items()
372-
if not emoji['deactivated']}
374+
if not emoji['deactivated']} # type: NamedEmojiData
373375
return custom_emojis
374376

375377
def get_messages(self, *,

0 commit comments

Comments
 (0)