-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python 3.11 has changed behavior of __str__ for IntEnum. Before it would return the name, like "Color.RED", now it's equivalent to str(self.value), for example "1".__repr__ is unchanged [1]. Python 3.11 also introduced StrEnum, which we can use instead of mixing in str type for our StrEnumDefinition, and that introcudes the same change of __str__ behavior: it returns the value as string. The corresponds, in spirit, to what I often did in Python: class Color(Enum): RED="red" BLUE="blue" def __str__(self): return self.value To ensure consistent behavior for different Python versions, and because we can determine our own rules for the enum types we define, we mimic the 3.11 __str__ behavior on IntEnumDefinition and StrEnumDefinition. We use the same trick, by setting the __str__ property to the version of the underlying type: str.__str__ and int.__repr__. int.__str__ won't do because int directly inherits object.__str__ that basically repr(self), and thus back to Enum.__repr__. So we need int.__repr__ to return the integer as string. [1] python/cpython#84247
- Loading branch information
Showing
3 changed files
with
80 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters