From: Christian Heller Date: Mon, 4 Aug 2025 04:55:41 +0000 (+0200) Subject: Dataclass classes pylint complains about having too few public methods. X-Git-Url: https://plomlompom.com/repos/booking/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/add_structured?a=commitdiff_plain;h=HEAD;p=ircplom Dataclass classes pylint complains about having too few public methods. --- diff --git a/ircplom/events.py b/ircplom/events.py index 339b1fc..e06ce34 100644 --- a/ircplom/events.py +++ b/ircplom/events.py @@ -1,14 +1,17 @@ 'Event system with event loop.' from abc import abstractmethod, ABC +from dataclasses import dataclass from queue import SimpleQueue, Empty as QueueEmpty from threading import Thread from typing import Any, Iterator, Literal, Self +@dataclass class Event: 'Communication unit between threads.' +@dataclass class AffectiveEvent(Event, ABC): 'For Events that are to affect other objects.' @@ -17,32 +20,30 @@ class AffectiveEvent(Event, ABC): 'To be run by main loop on target.' +@dataclass class PayloadMixin: 'Extends with .payload= passed as first argument.' - - def __init__(self, payload, **kwargs) -> None: - super().__init__(**kwargs) - self.payload = payload + payload: Any +@dataclass class ExceptionEvent(Event, PayloadMixin): 'To deliver Exception to main loop for handling.' payload: Exception +@dataclass class QuitEvent(Event): 'To break main loop towards.' +@dataclass class QueueMixin: - 'Adds SimpleQueue addressable via ._put(Event).' - - def __init__(self, q_out: SimpleQueue, **kwargs) -> None: - self._q_out = q_out - super().__init__(**kwargs) + 'Adds SimpleQueue .q_out addressable via ._put(Event).' + q_out: SimpleQueue def _put(self, event: Event) -> None: - self._q_out.put(event) + self.q_out.put(event) class Loop(QueueMixin): diff --git a/ircplom/irc_conn.py b/ircplom/irc_conn.py index 3146693..d2ba858 100644 --- a/ircplom/irc_conn.py +++ b/ircplom/irc_conn.py @@ -129,14 +129,13 @@ class IrcMessage: return self._raw +@dataclass class ClientIdMixin: 'Collects a Client\'s ID at .client_id.' - - def __init__(self, client_id: UUID, **kwargs) -> None: - super().__init__(**kwargs) - self.client_id = client_id + client_id: UUID +@dataclass class NewClientEvent(AffectiveEvent, PayloadMixin): 'Put Client .payload into ClientsDb target.' payload: 'Client' @@ -145,10 +144,12 @@ class NewClientEvent(AffectiveEvent, PayloadMixin): target[self.payload.id_] = self.payload +@dataclass class ClientEvent(AffectiveEvent, ClientIdMixin): 'To affect Client identified by ClientIdMixin.' +@dataclass class _ConnectedEvent(ClientEvent): def affect(self, target: 'Client') -> None: @@ -159,6 +160,7 @@ class _ConnectedEvent(ClientEvent): target.send(IrcMessage(verb='NICK', params=(target.nickname,))) +@dataclass class InitReconnectEvent(ClientEvent): 'To trigger re-opening of connection.' @@ -170,6 +172,7 @@ class InitReconnectEvent(ClientEvent): target.start_connecting() +@dataclass class SendEvent(ClientEvent, PayloadMixin): 'To trigger sending of payload to server.' payload: IrcMessage @@ -182,6 +185,7 @@ class SendEvent(ClientEvent, PayloadMixin): target.send(msg=self.payload, chat=self._chat) +@dataclass class ClientQueueMixin(QueueMixin): 'To QueueMixin adds _cput to extend ._put with client_id= setting.' client_id_name = 'id_' @@ -241,7 +245,7 @@ class Client(ABC, ClientQueueMixin): self._socket.settimeout(_TIMEOUT_RECV_LOOP) self.assumed_open = True self._recv_loop = Loop(iterator=self._read_lines(), - q_out=self._q_out) + q_out=self.q_out) self._cput(_ConnectedEvent) except Exception as e: # pylint: disable=broad-exception-caught self._put(ExceptionEvent(e)) @@ -366,6 +370,7 @@ class Client(ABC, ClientQueueMixin): bytes_total = b'' +@dataclass class _RecvEvent(ClientEvent, PayloadMixin): payload: str diff --git a/ircplom/tui.py b/ircplom/tui.py index 9888fa6..f0244a2 100644 --- a/ircplom/tui.py +++ b/ircplom/tui.py @@ -3,6 +3,7 @@ from abc import ABC, abstractmethod from base64 import b64decode from contextlib import contextmanager +from dataclasses import dataclass from inspect import _empty as inspect_empty, signature, stack from signal import SIGWINCH, signal from typing import Callable, Generator, Iterator, NamedTuple, Optional @@ -50,6 +51,7 @@ _CMD_SHORTCUTS = { } +@dataclass class TuiEvent(AffectiveEvent): 'To affect TUI, and trigger flushed .draw_tainted on it.' @@ -58,6 +60,7 @@ class TuiEvent(AffectiveEvent): target.term.flush() +@dataclass class _SetScreenEvent(TuiEvent): def affect(self, target: 'Tui') -> None: @@ -367,6 +370,7 @@ class _Window(_Widget): widget.draw() +@dataclass class _KeyboardEvent(TuiEvent, PayloadMixin): payload: str @@ -434,7 +438,7 @@ class Tui(QueueMixin): def _new_client_window(self, client_id: UUID, chat: str = '' ) -> '_ClientWindow': new_idx = len(self.windows) - win = _ClientWindow(idx=new_idx, term=self.term, q_out=self._q_out, + win = _ClientWindow(idx=new_idx, term=self.term, q_out=self.q_out, client_id=client_id, chat=chat) self.windows += [win] self._switch_window(new_idx) @@ -475,7 +479,7 @@ class Tui(QueueMixin): ) -> None: 'Create Client and pass it via NewClientEvent.' self._put(NewClientEvent( - _ClientKnowingTui(q_out=self._q_out, hostname=hostname, + _ClientKnowingTui(q_out=self.q_out, hostname=hostname, nickname=nickname, realname=realname))) def cmd__prompt_enter(self) -> None: @@ -552,7 +556,7 @@ class Terminal(QueueMixin): with (self._blessed.raw(), self._blessed.fullscreen(), self._blessed.hidden_cursor(), - Loop(iterator=self._get_keypresses(), q_out=self._q_out)): + Loop(iterator=self._get_keypresses(), q_out=self.q_out)): yield self @property @@ -675,13 +679,12 @@ class _ClientWindow(_Window, ClientQueueMixin): payload=IrcMessage(verb='PRIVMSG', params=(target, msg))) +@dataclass class _ClientWindowEvent(TuiEvent, ClientIdMixin): - - def __init__(self, chat: str = '', **kwargs) -> None: - self.chat = chat - super().__init__(**kwargs) + chat: str = '' +@dataclass class _ClientLogEvent(_ClientWindowEvent, PayloadMixin): payload: str @@ -695,6 +698,7 @@ class _ClientLogEvent(_ClientWindowEvent, PayloadMixin): super().affect(target) +@dataclass class _ClientPromptEvent(_ClientWindowEvent, PayloadMixin): payload: tuple[str, str]