Refactor magic strings to Enum and Constants
Why: avoid bugs and simplify future renamings.
For example we just fixed a bug caused by in one param we sent "e-mail" and in another "email". (Commit: 55bc2c40)
Define module constants or Enums to prevent this kind of bugs, eg:
- Define utility class StrEnum:
# module utils.py
from enum import Enum
class StrEnum(str, Enum):
"""
Enum where members are also (and must be) strings
"""
def __str__(self):
return str(self.value)
- Use StrEnum:
# module data_source.py
class DataSourceResponse(StrEnum):
USER_ID='user_id'
EMAIL ='email'
from data_source import DataSourceResponse
print(DataSourceResponse.EMAIL)
# prints email
- Or use Constants:
# module data_source.py import DataSource
class DataSource:
EMAIL ='email'
from data_source import DataSource
print(DataSource.EMAIL)
# prints email
- However usage of enums allows to group constants by meaning, for example:
# module router.py
HEADER_MESSAGE_ID = "message-id"
class MessageInputKeys(StrEnum):
TARGET='target'
PRIMARYKEY ='primaryKey'
class MessageKeys(StrEnum):
CHANNEL_ID='channel_id'
PRIORITY ='priority'
Edited by Carina Antunes