I need to send messages to an api. There are 2 different types of messages: create or update. Create messages will create an entry in a database. Update messages will update an entry in a database.
Here's the code I've used to implement these as classes:
from abc import ABCMeta, abstractmethod
from dataclasses import asdict, dataclass
@dataclass
class Message(metaclass=ABCMeta):
message_type: str
def to_dict(self) -> dict:
return asdict(self)
@dataclass
class CreateMessage(Message):
message_id: str
message_status: str
@dataclass
class UpdateMessage(Message):
message_id: str
new_message_status: str
However, my coworker said name of these classes sound more like a function. Do you guys have any suggestions of what I could name the UpdateMessage and CreateMessage classes to be less confusing? He suggested UpdateMessageDto and CreateMessageDto, but I'm concerned that other people won't know what a Dto is.
I agree that CreateMessage
for example seems like that would perform as a function. I would suggest ensuring the name uses a noun instead, if possible, since classes generally are objects which contain data, which makes them noticeably different from function which are generally associated with a verb, for example like create_message
.
I'd suggest tacking on something like Action
to the model class name. Another option is to also be more explicit and add a noun like Model
. In case you find that including the phrase "Message" becomes a bit redundant, I'd also suggest removing the Message
part of the class, for example having it like CreateModel
, and then you can put it in a module called messages.py
and that way the from messages import CreateModel
will make it clear that CreateModel
is a Message model class.
The below example illustrates one of the possible naming schemas I'd suggest:
from __future__ import annotations
from abc import ABCMeta
from dataclasses import asdict, dataclass
from typing import Any
@dataclass
class Message(ABCMeta):
message_type: str
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass
class CreateModel(Message):
message_id: str
message_status: str
@dataclass
class UpdateModel(Message):
message_id: str
new_message_status: str