home · contact · privacy
Slightly refactor ClientDb type hints.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 14 Aug 2025 12:30:27 +0000 (14:30 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 14 Aug 2025 12:30:27 +0000 (14:30 +0200)
ircplom/client.py
ircplom/client_tui.py

index 5aa87bbf9b42ab5080c24d553279d3745f3facce..f33e42e76a8c35574de0517d5090da31212054f2 100644 (file)
@@ -14,6 +14,7 @@ from ircplom.irc_conn import (BaseIrcConnection, IrcConnAbortException,
                               IrcMessage, PORT_SSL)
 
 ClientsDb = dict[str, 'Client']
+ClientDbType = int | str | list[str]
 _NAMES_DESIRED_SERVER_CAPS = ('server-time', 'account-tag', 'sasl')
 
 # NB: in below numerics accounting, tuples define inclusive ranges
@@ -177,7 +178,7 @@ class ClientDb:
     'Optimized for dealing with variable confirmation of values.'
 
     def __init__(self) -> None:
-        self._dict: dict[str, int | str | list[str]] = {}
+        self._dict: dict[str, ClientDbType] = {}
         self._confirmeds: list[str] = []
 
     def __getattr__(self, key: str) -> Any:
@@ -185,10 +186,7 @@ class ClientDb:
             return self._dict[key]
         return None
 
-    def set(self,
-            key: str,
-            value: Optional[int | str | list[str]],
-            confirm=False
+    def set(self, key: str, value: Optional[ClientDbType], confirm=False
             ) -> tuple[bool, bool]:
         'Ensures setting, returns if changed value or confirmation.'
         old_value, was_confirmed = self.get_force(key)
@@ -221,8 +219,7 @@ class ClientDb:
         else:
             raise CrashingException('called on non-list entry')
 
-    def get_force(self, key: str) -> tuple[Optional[int | str | list[str]],
-                                           bool]:
+    def get_force(self, key: str) -> tuple[Optional[ClientDbType], bool]:
         'Get even if only stored unconfirmed, tell if confirmed..'
         return (self._dict.get(key, None), key in self._confirmeds)
 
@@ -316,10 +313,7 @@ class Client(ABC, ClientQueueMixin):
                 self._log(to_log, scope=log_target)
         self._log(msg.raw, scope=LogScope.RAW, out=True)
 
-    def _update_db(self,
-                   key: str,
-                   value: Optional[int | str | list[str]],
-                   confirm: bool
+    def _update_db(self, key: str, value: Optional[ClientDbType], confirm: bool
                    ) -> tuple[bool, bool]:
         'Wrap ._db.set into something accessible to subclass extension.'
         return self._db.set(key, value, confirm)
index d568a6ce3c3fb7680b9f5ec2923169ac1c28a99a..1e1c4c84e1dfd8c94d76bad51081a4c4b0073f89 100644 (file)
@@ -7,8 +7,8 @@ from typing import Callable, Optional, Sequence
 from ircplom.tui_base import (BaseTui, PromptWidget, TuiEvent, Window,
                               CMD_SHORTCUTS)
 from ircplom.irc_conn import IrcMessage
-from ircplom.client import (IrcConnSetup, Client, ClientDb, ClientQueueMixin,
-                            LogScope, NewClientEvent)
+from ircplom.client import (IrcConnSetup, Client, ClientDb, ClientDbType,
+                            ClientQueueMixin, LogScope, NewClientEvent)
 
 CMD_SHORTCUTS['disconnect'] = 'window.disconnect'
 CMD_SHORTCUTS['join'] = 'window.join'
@@ -169,7 +169,7 @@ class _ClientWindowsManager:
 
     def update(self,
                key: str,
-               value: Optional[str],
+               value: Optional[ClientDbType],
                confirmed: bool,
                scope: LogScope
                ) -> bool:
@@ -291,10 +291,7 @@ class _ClientKnowingTui(Client):
                 with open(f'{self.client_id}.log', 'a', encoding='utf8') as f:
                     f.write(('>' if kwargs['out'] else '<') + f' {msg}\n')
 
-    def _update_db(self,
-                   key: str,
-                   value: Optional[int | str | list[str]],
-                   confirm: bool
+    def _update_db(self, key: str, value: Optional[ClientDbType], confirm: bool
                    ) -> tuple[bool, bool]:
         value_changed, conf_changed = super()._update_db(key, value, confirm)
         if value is None and not value_changed:  # local ._db may have fallback