diff --git a/CHANGELOG.md b/CHANGELOG.md index 367c5cbc4..21bafce20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### Changes +- deltachat-rpc-client: use `dataclass` for `Account`, `Chat`, `Contact` and `Message` #4042 ### Fixes - deltachat-rpc-server: do not block stdin while processing the request. #4041 diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/account.py b/deltachat-rpc-client/src/deltachat_rpc_client/account.py index 44535da08..63a9b169c 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/account.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/account.py @@ -1,4 +1,5 @@ from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from dataclasses import dataclass from ._utils import AttrDict from .chat import Chat @@ -11,28 +12,17 @@ if TYPE_CHECKING: from .deltachat import DeltaChat +@dataclass class Account: """Delta Chat account.""" - def __init__(self, manager: "DeltaChat", account_id: int) -> None: - self.manager = manager - self.id = account_id + manager: "DeltaChat" + id: int @property def _rpc(self) -> Rpc: return self.manager.rpc - def __eq__(self, other) -> bool: - if not isinstance(other, Account): - return False - return self.id == other.id and self.manager == other.manager - - def __ne__(self, other) -> bool: - return not self == other - - def __repr__(self) -> str: - return f"" - async def wait_for_event(self) -> AttrDict: """Wait until the next event and return it.""" return AttrDict(await self._rpc.wait_for_event(self.id)) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/chat.py b/deltachat-rpc-client/src/deltachat_rpc_client/chat.py index 73adbfb1c..c2e5ca364 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/chat.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/chat.py @@ -1,6 +1,7 @@ import calendar from datetime import datetime from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from dataclasses import dataclass from ._utils import AttrDict from .const import ChatVisibility @@ -12,28 +13,17 @@ if TYPE_CHECKING: from .account import Account +@dataclass class Chat: """Chat object which manages members and through which you can send and retrieve messages.""" - def __init__(self, account: "Account", chat_id: int) -> None: - self.account = account - self.id = chat_id + account: "Account" + id: int @property def _rpc(self) -> Rpc: return self.account._rpc - def __eq__(self, other) -> bool: - if not isinstance(other, Chat): - return False - return self.id == other.id and self.account == other.account - - def __ne__(self, other) -> bool: - return not self == other - - def __repr__(self) -> str: - return f"" - async def delete(self) -> None: """Delete this chat and all its messages. diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/contact.py b/deltachat-rpc-client/src/deltachat_rpc_client/contact.py index 7c5267b4f..7999d59ed 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/contact.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/contact.py @@ -1,4 +1,5 @@ from typing import TYPE_CHECKING +from dataclasses import dataclass from ._utils import AttrDict from .rpc import Rpc @@ -8,6 +9,7 @@ if TYPE_CHECKING: from .chat import Chat +@dataclass class Contact: """ Contact API. @@ -15,20 +17,8 @@ class Contact: Essentially a wrapper for RPC, account ID and a contact ID. """ - def __init__(self, account: "Account", contact_id: int) -> None: - self.account = account - self.id = contact_id - - def __eq__(self, other) -> bool: - if not isinstance(other, Contact): - return False - return self.id == other.id and self.account == other.account - - def __ne__(self, other) -> bool: - return not self == other - - def __repr__(self) -> str: - return f"" + account: "Account" + id: int @property def _rpc(self) -> Rpc: diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/message.py b/deltachat-rpc-client/src/deltachat_rpc_client/message.py index d38d2cbfe..7784d8dac 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/message.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/message.py @@ -1,5 +1,6 @@ import json from typing import TYPE_CHECKING, Union +from dataclasses import dataclass from ._utils import AttrDict from .contact import Contact @@ -9,23 +10,12 @@ if TYPE_CHECKING: from .account import Account +@dataclass class Message: """Delta Chat Message object.""" - def __init__(self, account: "Account", msg_id: int) -> None: - self.account = account - self.id = msg_id - - def __eq__(self, other) -> bool: - if not isinstance(other, Message): - return False - return self.id == other.id and self.account == other.account - - def __ne__(self, other) -> bool: - return not self == other - - def __repr__(self) -> str: - return f"" + account: "Account" + id: int @property def _rpc(self) -> Rpc: