You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IntEnum __format__ behavior can't be overridden through __str__ #81660
Overriding __str__ in this way works as expected if the inherited classes are int or Enum individually. However, it does not work when inheriting (int, Enum) or when inheriting (intEnum).
I mistyped - __str__ can be overridden effectively, but __format__ has to be overridden instead or overridden also to get the desired f-string behavior.
changed the title [-]IntEnum f-string behavior can't be overridden[/-][+]IntEnum __format__ behavior can't be overridden through __str__[/+]on Jul 2, 2019
New changeset 2f19e82 by Ethan Furman (thatneat) in branch 'master': bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__ (GH-14545) 2f19e82
is valid in python 3.7 but due to the change not valid in python 3.8+ anymore.
Since "str" can't be represented by "{:X}".
Since isinstance(t, int) is true in both scenarios, it is to be expected that f"0x{t:X}" will hold. it is an "int" and not a "str"
The str function exists in both cases, the override of the second case shall not alter the behavior of the class !
Activity
thatneat commentedon Jul 1, 2019
Combining int and Enum, as with enum.IntEnum results in a class where __str__ cannot be effectively overridden. For example:
Expected output:
'aaaaAAAa, aaaaAAAa'
Actual output:
'1, aaaaAAAa'
Overriding __str__ in this way works as expected if the inherited classes are int or Enum individually. However, it does not work when inheriting (int, Enum) or when inheriting (intEnum).
Presumably this is a side effect of Enum's mixin behavior documented at https://docs.python.org/3/library/enum.html#others and it is possibly related to https://bugs.python.org/issue18264 .
thatneat commentedon Jul 1, 2019
I mistyped - __str__ can be overridden effectively, but __format__ has to be overridden instead or overridden also to get the desired f-string behavior.
thatneat commentedon Jul 1, 2019
related cPython code:
cpython/Lib/enum.py
Line 626 in 19a1e1e
If we're in a mixed-in class but the class has overridden __str__(), we should probably use str(self) instead of self._value_.
ericvsmith commentedon Jul 2, 2019
Note that this isn't really related to f-strings, except that they use the __format__ protocol, as does str.__format__.
>>> format(myIntEnum.x) '1'[-]IntEnum f-string behavior can't be overridden[/-][+]IntEnum __format__ behavior can't be overridden through __str__[/+]ethanfurman commentedon Jul 4, 2019
New changeset 2f19e82 by Ethan Furman (thatneat) in branch 'master':
bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__ (GH-14545)
2f19e82
ethanfurman commentedon Sep 13, 2020
New changeset 38c8d39 by Ethan Furman in branch '3.8':
[3.8] bpo-37479: Enum - use correct __format__ (GH-14545)
38c8d39
ethanfurman commentedon Sep 13, 2020
Thank you, Jason!
zinnjonas commentedon Nov 23, 2022
Was this change really intended?
The change will make projects break in their behavior when changed from 3.7 to a newer version.
is valid python code and also compatible with python 3.7.
But!!!
is valid in python 3.7 but due to the change not valid in python 3.8+ anymore.
Since "str" can't be represented by "{:X}".
Since
isinstance(t, int)is true in both scenarios, it is to be expected that f"0x{t:X}" will hold. it is an "int" and not a "str"The str function exists in both cases, the override of the second case shall not alter the behavior of the class !
ethanfurman commentedon Nov 23, 2022
@zinnjonas This has been corrected in 3.11.