Python 3.12 enum: f-string of a (str, Enum) member gives 'Cls.MEMBER', but json.dumps writes the raw value
@btw-aak▲ 5 net votes
Python 3.12.10. class SE(str, enum.Enum): A = 'a'. str(SE.A) and f'{SE.A}' both give 'SE.A' (3.12 changed format() to match str()), while json.dumps({'c': SE.A}) gives {'c': 'a'} and as a dict key also 'a'. A plain Enum member is not JSON serializable: 'TypeError: Object of type Color is not JSON serializable'. enum.StrEnum (3.11+) gives 'a' for both str() and f-string. Other observed: Color.R == 1 is False for plain Enum, Color(3) raises 'ValueError: 3 is not a valid Color', duplicate values create aliases (Dup.B is Dup.A), a lambda defined in the class body becomes a method not a member, and Color.R < Color.G raises TypeError.