from enum import Enum
class ErrorCode(str, Enum):
GENERAL_ERROR = 'A general error has occurred.'
INVALID_RECIPIENT_EMAIL_ADDRESS = 'The recipient email address provided is not a valid email address.'
@classmethod
def addErrorsAsAttrib(cls, err_code, err_description):
setattr(cls, err_code, err_description)
extended_error_codes = ErrorCode.addErrorsAsAttrib('NEW_ERROR2', Enum('NEW_ERROR2', 'The new error 2'))
print(ErrorCode.__members__.keys())
# OUTPUT:
# dict_keys(['GENERAL_ERROR', 'INVALID_RECIPIENT_EMAIL_ADDRESS'])
I am trying to find a way to dynamically add new Error Codes to my ErrorCode class (an Enum derived class) but just cannot determine the correct way to do this. As per the code sample - I tried setattr() but this does not perform as expected. Any help would be appreciated.