Repository: zulip/zulip-terminal

Issue:
[bug] crash when editing message
Server: Zulip cloud server
Client: main branch
Python: 3.11
self.model.initial_data: `{'zulip_version': '9.0-dev-642-g9d469357ea', 'zulip_feature_level': 237, 'zulip_merge_base': '9.0-dev-605-g47a5459637', 'realm_message_content_edit_limit_seconds': None}`
log:
```
Traceback (most recent call last):
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/zulipterminal/cli/run.py", line 578, in main
    ).main()
      ^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/zulipterminal/core.py", line 691, in main
    self.loop.run()
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/main_loop.py", line 287, in run
    self._run()
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/main_loop.py", line 385, in _run
    self.event_loop.run()
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/main_loop.py", line 790, in run
    self._loop()
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/main_loop.py", line 827, in _loop
    self._watch_files[fd]()
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/raw_display.py", line 416, in <lambda>
    wrapper = lambda: self.parse_input(
                      ^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/raw_display.py", line 515, in parse_input
    callback(processed, processed_codes)
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/main_loop.py", line 412, in _update
    self.process_input(keys)
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/main_loop.py", line 513, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/zulipterminal/ui.py", line 324, in keypress
    return super().keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/container.py", line 1135, in keypress
    return self.body.keypress( (maxcol, remaining), key )
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/container.py", line 2316, in keypress
    key = w.keypress((mc,) + size[1:], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/container.py", line 1626, in keypress
    key = self.focus.keypress(tsize, key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/container.py", line 2316, in keypress
    key = w.keypress((mc,) + size[1:], key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/zulipterminal/ui_tools/views.py", line 651, in keypress
    return super().keypress(size, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/container.py", line 1135, in keypress
    return self.body.keypress( (maxcol, remaining), key )
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/zulipterminal/ui_tools/views.py", line 249, in keypress
    key = super().keypress(size, key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/urwid/listbox.py", line 968, in keypress
    key = focus_widget.keypress((maxcol,),key)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/.local/share/pipx/venvs/zulip-term/lib/python3.11/site-packages/zulipterminal/ui_tools/messages.py", line 1048, in keypress
    if self.model.initial_data["realm_message_content_edit_limit_seconds"] > 0:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'
```

Discussion:
neiljp: @Young-Lord Thanks for finding the bug and providing this fix! :)

I can add a test case for this situation before merging, unless you wish to do so?
Young-Lord: I don't know how to add a test case, so maybe you can do that.
neiljp: No problem :)
neiljp: @Young-Lord Thanks again for finding and fixing! I slightly updated your commit text, and added a commit which covers testing of the new behavior, and am merging now 🎉 

Patch:
diff --git a/zulipterminal/ui_tools/messages.py b/zulipterminal/ui_tools/messages.py
index 1a9bb9d..3ddfa10 100644
--- a/zulipterminal/ui_tools/messages.py
+++ b/zulipterminal/ui_tools/messages.py
@@ -1045,12 +1045,12 @@ class MessageBox(urwid.Pile):
             # the time limit. A limit of 0 signifies no limit
             # on message body editing.
             msg_body_edit_enabled = True
-            if self.model.initial_data["realm_message_content_edit_limit_seconds"] > 0:
+            edit_time_limit = self.model.initial_data[
+                "realm_message_content_edit_limit_seconds"
+            ]
+            if edit_time_limit is not None and edit_time_limit > 0:
                 if self.message["sender_id"] == self.model.user_id:
                     time_since_msg_sent = time() - self.message["timestamp"]
-                    edit_time_limit = self.model.initial_data[
-                        "realm_message_content_edit_limit_seconds"
-                    ]
                     # Don't allow editing message body if time-limit exceeded.
                     if time_since_msg_sent >= edit_time_limit:
                         if self.message["type"] == "private":


Test patch:
diff --git a/tests/ui_tools/test_messages.py b/tests/ui_tools/test_messages.py
index d4d9e80..8deb4eb 100644
--- a/tests/ui_tools/test_messages.py
+++ b/tests/ui_tools/test_messages.py
@@ -1301,7 +1301,16 @@ class TestMessageBox:
                 {"stream": True, "private": True},
                 {"stream": True, "private": True},
                 {"stream": None, "private": None},
-                id="no_msg_body_edit_limit",
+                id="no_msg_body_edit_limit:ZFL<138",
+            ),
+            case(
+                {"sender_id": 1, "timestamp": 1, "subject": "test"},
+                True,
+                None,
+                {"stream": True, "private": True},
+                {"stream": True, "private": True},
+                {"stream": None, "private": None},
+                id="no_msg_body_edit_limit:ZFL>=138",
             ),
             case(
                 {"sender_id": 1, "timestamp": 1, "subject": "(no topic)"},
@@ -1352,7 +1361,16 @@ class TestMessageBox:
                 {"stream": True, "private": True},
                 {"stream": True, "private": True},
                 {"stream": None, "private": None},
-                id="no_msg_body_edit_limit_with_no_topic",
+                id="no_msg_body_edit_limit_with_no_topic:ZFL<138",
+            ),
+            case(
+                {"sender_id": 1, "timestamp": 45, "subject": "(no topic)"},
+                True,
+                None,
+                {"stream": True, "private": True},
+                {"stream": True, "private": True},
+                {"stream": None, "private": None},
+                id="no_msg_body_edit_limit_with_no_topic:ZFL>=138",
             ),
         ],
     )