mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
bpo-41816: StrEnum.__str__
is str.__str__
(GH-22362)
use `str.__str__` for `StrEnum` so that `str(StrEnum.member)` is the same as directly accessing the string value of the `StrEnum` member
This commit is contained in:
parent
947adcaa13
commit
d986d1657e
3 changed files with 48 additions and 0 deletions
|
@ -2036,6 +2036,37 @@ class TestEnum(unittest.TestCase):
|
|||
two = '2'
|
||||
three = b'3', 'ascii'
|
||||
four = b'4', 'latin1', 'strict'
|
||||
self.assertEqual(GoodStrEnum.one, '1')
|
||||
self.assertEqual(str(GoodStrEnum.one), '1')
|
||||
self.assertEqual(GoodStrEnum.one, str(GoodStrEnum.one))
|
||||
self.assertEqual(GoodStrEnum.one, '{}'.format(GoodStrEnum.one))
|
||||
#
|
||||
class DumbMixin:
|
||||
def __str__(self):
|
||||
return "don't do this"
|
||||
class DumbStrEnum(DumbMixin, StrEnum):
|
||||
five = '5'
|
||||
six = '6'
|
||||
seven = '7'
|
||||
self.assertEqual(DumbStrEnum.seven, '7')
|
||||
self.assertEqual(str(DumbStrEnum.seven), "don't do this")
|
||||
#
|
||||
class EnumMixin(Enum):
|
||||
def hello(self):
|
||||
print('hello from %s' % (self, ))
|
||||
class HelloEnum(EnumMixin, StrEnum):
|
||||
eight = '8'
|
||||
self.assertEqual(HelloEnum.eight, '8')
|
||||
self.assertEqual(HelloEnum.eight, str(HelloEnum.eight))
|
||||
#
|
||||
class GoodbyeMixin:
|
||||
def goodbye(self):
|
||||
print('%s wishes you a fond farewell')
|
||||
class GoodbyeEnum(GoodbyeMixin, EnumMixin, StrEnum):
|
||||
nine = '9'
|
||||
self.assertEqual(GoodbyeEnum.nine, '9')
|
||||
self.assertEqual(GoodbyeEnum.nine, str(GoodbyeEnum.nine))
|
||||
#
|
||||
with self.assertRaisesRegex(TypeError, '1 is not a string'):
|
||||
class FirstFailedStrEnum(StrEnum):
|
||||
one = 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue