From e38abfbcb9e31594224d7415542fb8ea4e2f9fa6 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Tue, 22 Sep 2020 07:42:31 -0700 Subject: [PATCH 1/3] StrEnum.__str__ is str.__str__ --- Doc/library/enum.rst | 15 +++++++++++++++ Lib/enum.py | 2 ++ Lib/test/test_enum.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst index 843d961afc4f72..68446885f3119c 100644 --- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -622,6 +622,11 @@ an incorrect member:: Before :class:`StrEnum`, ``Directions.NORTH`` would have been the :class:`tuple` ``('north',)``. +.. note:: + + Unlike other Enum's, ``str(StrEnum.member)`` will return the value of the + member instead of the usual ``"EnumClass.member"``. + .. versionadded:: 3.10 @@ -1217,3 +1222,13 @@ all named flags and all named combinations of flags that are in the value:: >>> Color(7) # not named combination +``StrEnum`` and :meth:`str.__str__` +""""""""""""""""""""""""""""""""""" + +An important difference between :class:`StrEnum` and other Enums is the +:meth:`__str__` method; because :class:`StrEnum` members are strings, some +parts of Python will read the string data directly, while others will call +:meth:`str()`. To make those two operations have the same result, +:meth:`StrEnum.__str__` will be the same as :meth:`str.__str__` so that +``str(StrEnum.member) == StrEnum.member`` is true. + diff --git a/Lib/enum.py b/Lib/enum.py index 589b17fd697775..40ff25b9cdad37 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -718,6 +718,8 @@ def __new__(cls, *values): member._value_ = value return member + __str__ = str.__str__ + def _reduce_ex_by_name(self, proto): return self.name diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 8e84d929429ebf..b0dd7a82379513 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2036,6 +2036,35 @@ class GoodStrEnum(StrEnum): two = '2' three = b'3', 'ascii' four = b'4', 'latin1', 'strict' + self.assertEqual(GoodStrEnum.one, '1') + self.assertEqual(GoodStrEnum.one, str(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 From b2e69238ec02e86715a341850fcdf302a1b335e0 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Tue, 22 Sep 2020 10:59:39 -0700 Subject: [PATCH 2/3] add format test --- Lib/test/test_enum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index b0dd7a82379513..b4c939c17e1a3b 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2038,6 +2038,7 @@ class GoodStrEnum(StrEnum): four = b'4', 'latin1', 'strict' self.assertEqual(GoodStrEnum.one, '1') self.assertEqual(GoodStrEnum.one, str(GoodStrEnum.one)) + self.assertEqual(GoodStrEnum.one, '{}'.format(GoodStrEnum.one)) # class DumbMixin: def __str__(self): From 4a4867c3ffc052013f3a77e7c6803d7b3f1cbdce Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Tue, 22 Sep 2020 12:07:30 -0700 Subject: [PATCH 3/3] add another test --- Lib/test/test_enum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index b4c939c17e1a3b..3431040f98a726 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2037,6 +2037,7 @@ class GoodStrEnum(StrEnum): 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)) #