Skip to content

Commit 7819dab

Browse files
israelsgalaxyneiljp
authored andcommitted
refactor: messages: Change OrderedDict to Dict in MessageBox.
message_links and topic_links are declared as Ordered Dicts to preserve their insertion order when used in rendering a MessageBox. transform_content creates an empty OrderedDict and calls soup2markup to populate it with message links. footlinks_view then renders the message links in a MessageBox. Note this occurs after the popup changes, so the ordering should be preserved already; this is input to those views. Since Dict also preserves insertion order from python3.7, we can safely declare message_links and topic_links as Dicts without any change in behavior. Fixes #1330.
1 parent 91ed1e7 commit 7819dab

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

zulipterminal/ui_tools/messages.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import typing
6-
from collections import OrderedDict, defaultdict
6+
from collections import defaultdict
77
from datetime import date, datetime
88
from time import time
99
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, Union
@@ -55,8 +55,8 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
5555
self.topic_name = ""
5656
self.email = "" # FIXME: Can we remove this?
5757
self.user_id: Optional[int] = None
58-
self.message_links: "OrderedDict[str, Tuple[str, int, bool]]" = OrderedDict()
59-
self.topic_links: "OrderedDict[str, Tuple[str, int, bool]]" = OrderedDict()
58+
self.message_links: Dict[str, Tuple[str, int, bool]] = dict()
59+
self.topic_links: Dict[str, Tuple[str, int, bool]] = dict()
6060
self.time_mentions: List[Tuple[str, str]] = list()
6161
self.last_message = last_message
6262
# if this is the first message
@@ -295,11 +295,9 @@ def reactions_view(self, reactions: List[Dict[str, Any]]) -> Any:
295295
except Exception:
296296
return ""
297297

298-
# Use quotes as a workaround for OrderedDict typing issue.
299-
# See https://github.com/python/mypy/issues/6904.
300298
@staticmethod
301299
def footlinks_view(
302-
message_links: "OrderedDict[str, Tuple[str, int, bool]]",
300+
message_links: Dict[str, Tuple[str, int, bool]],
303301
*,
304302
maximum_footlinks: int,
305303
padded: bool,
@@ -357,9 +355,7 @@ def footlinks_view(
357355
@classmethod
358356
def soup2markup(
359357
cls, soup: Any, metadata: Dict[str, Any], **state: Any
360-
) -> Tuple[
361-
List[Any], "OrderedDict[str, Tuple[str, int, bool]]", List[Tuple[str, str]]
362-
]:
358+
) -> Tuple[List[Any], Dict[str, Tuple[str, int, bool]], List[Tuple[str, str]]]:
363359
# Ensure a string is provided, in case the soup finds none
364360
# This could occur if eg. an image is removed or not shown
365361
markup: List[Union[str, Tuple[Optional[str], Any]]] = [""]
@@ -807,15 +803,15 @@ def transform_content(
807803
cls, content: Any, server_url: str
808804
) -> Tuple[
809805
Tuple[None, Any],
810-
"OrderedDict[str, Tuple[str, int, bool]]",
806+
Dict[str, Tuple[str, int, bool]],
811807
List[Tuple[str, str]],
812808
]:
813809
soup = BeautifulSoup(content, "lxml")
814810
body = soup.find(name="body")
815811

816812
metadata = dict(
817813
server_url=server_url,
818-
message_links=OrderedDict(),
814+
message_links=dict(),
819815
time_mentions=list(),
820816
) # type: Dict[str, Any]
821817

0 commit comments

Comments
 (0)