The problem is that you’ve nested the definition of your exception class inside an enum:
class Weekdays(Enum): MONDAY = 'mon' TUESDAY = 'tue' WEDNESDAY = 'wed' THURSDAY = 'thu' FRIDAY = 'fri' class InvalidValue(Exception): pass
enum’s (through metaclass chicanery) make their class attributes singleton instances of the enum class, with the value you assigned to the attribute in the definition inside the
.value of that enum instance. Just like the other class attributes you defined in your enum. So, consider:
In : from enum import Enum In : ...: class Weekdays(Enum): ...: MONDAY = 'mon' ...: TUESDAY = 'tue' ...: WEDNESDAY = 'wed' ...: THURSDAY = 'thu' ...: FRIDAY = 'fri' ...: ...: class InvalidValue(Exception): ...: pass ...: In : Weekdays.MONDAY Out: <Weekdays.MONDAY: 'mon'> In : Weekdays.MONDAY.value Out: 'mon' In : Weekdays.InvalidValue Out: <Weekdays.InvalidValue: <class '__main__.Weekdays.InvalidValue'>> In : Weekdays.InvalidValue.value Out: __main__.Weekdays.InvalidValue
So, you could use:
with self.assertRaises(Weekdays.InvalidValue.value): ...
And in similarly, in
parse, you need:
raise Weekdays.InvalidValue.value() from e
But you are better off just defining
InvalidValue at the module level.
CLICK HERE to find out more related problems solutions.