home · contact · privacy
Don't force dataclass'ness on update values.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 11 Sep 2025 21:03:54 +0000 (23:03 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 11 Sep 2025 21:03:54 +0000 (23:03 +0200)
ircplom/client_tui.py

index b07a532d0b3cf52fda68c3da4146bff76a06a989..c4ea133d9075048cd0c5bfbfed4c1c1faf7674e4 100644 (file)
@@ -1,8 +1,6 @@
 'TUI adaptions to Client.'
 # built-ins
 from getpass import getuser
-from dataclasses import asdict as dc_asdict
-from types import get_original_bases
 from typing import Any, Callable, Optional, Self, Sequence
 # ourselves
 from ircplom.tui_base import (BaseTui, PromptWidget, TuiEvent, Window,
@@ -29,10 +27,14 @@ class _Update:
 
     def __init__(self, path: tuple[str, ...], value: Any) -> None:
         self.path = path
-        for cls in [cls for cls in _UPDATING_DATACLASSES
-                    if isinstance(value, get_original_bases(cls)[1])]:
-            value = cls(**dc_asdict(value))
         self.value = value
+        for cls in [cls for cls in locals().values()
+                    if isinstance(cls, type)
+                    and issubclass(cls, _UpdatingNode)
+                    and cls is not type(value)
+                    and issubclass(cls, type(value))]:
+            self.value = cls()
+            self.value.copy_annotated_attrs(value)
 
     @property
     def key(self) -> str:
@@ -50,6 +52,12 @@ class _UpdatingNode(AutoAttrMixin):
     def _make_attr(self, cls: Callable, key: str):
         return cls()
 
+    def copy_annotated_attrs(self, original) -> None:
+        'From original copy those of its attributes annotated for self.'
+        for key in [k for k in self._deep_annotations()
+                    if hasattr(original, k)]:
+            setattr(self, key, getattr(original, key))
+
     @classmethod
     def _scope(cls, key: str) -> LogScope:
         scopes: dict[str, LogScope] = {}