diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2020-09-13 20:47:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-13 20:47:43 (GMT) |
commit | 38c8d3930eb872258a82339bcba3bce1d0e3ac2c (patch) | |
tree | cf9087f277d04c2ebf320a55f4f41cb4f7c51e2a /Lib/test | |
parent | bf50b0e80a8a0d651af2f953b662eeadd27c7c93 (diff) | |
download | cpython-38c8d3930eb872258a82339bcba3bce1d0e3ac2c.zip cpython-38c8d3930eb872258a82339bcba3bce1d0e3ac2c.tar.gz cpython-38c8d3930eb872258a82339bcba3bce1d0e3ac2c.tar.bz2 |
[3.8] bpo-37479: Enum - use correct __format__ (GH-14545)
* bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__.
(cherry picked from commit 2f19e82fbe98ce86bcd98a176328af2808b678e8)
Co-authored-by: thatneat <thatneat@users.noreply.github.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_enum.py | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index a2a3c56..0f91f00 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -445,12 +445,63 @@ class TestEnum(unittest.TestCase): self.assertEqual('{:<20}'.format(Season.SPRING), '{:<20}'.format(str(Season.SPRING))) - def test_format_enum_custom(self): + def test_str_override_enum(self): + class EnumWithStrOverrides(Enum): + one = auto() + two = auto() + + def __str__(self): + return 'Str!' + self.assertEqual(str(EnumWithStrOverrides.one), 'Str!') + self.assertEqual('{}'.format(EnumWithStrOverrides.one), 'Str!') + + def test_format_override_enum(self): + class EnumWithFormatOverride(Enum): + one = 1.0 + two = 2.0 + def __format__(self, spec): + return 'Format!!' + self.assertEqual(str(EnumWithFormatOverride.one), 'EnumWithFormatOverride.one') + self.assertEqual('{}'.format(EnumWithFormatOverride.one), 'Format!!') + + def test_str_and_format_override_enum(self): + class EnumWithStrFormatOverrides(Enum): + one = auto() + two = auto() + def __str__(self): + return 'Str!' + def __format__(self, spec): + return 'Format!' + self.assertEqual(str(EnumWithStrFormatOverrides.one), 'Str!') + self.assertEqual('{}'.format(EnumWithStrFormatOverrides.one), 'Format!') + + def test_str_override_mixin(self): + class MixinEnumWithStrOverride(float, Enum): + one = 1.0 + two = 2.0 + def __str__(self): + return 'Overridden!' + self.assertEqual(str(MixinEnumWithStrOverride.one), 'Overridden!') + self.assertEqual('{}'.format(MixinEnumWithStrOverride.one), 'Overridden!') + + def test_str_and_format_override_mixin(self): + class MixinWithStrFormatOverrides(float, Enum): + one = 1.0 + two = 2.0 + def __str__(self): + return 'Str!' + def __format__(self, spec): + return 'Format!' + self.assertEqual(str(MixinWithStrFormatOverrides.one), 'Str!') + self.assertEqual('{}'.format(MixinWithStrFormatOverrides.one), 'Format!') + + def test_format_override_mixin(self): class TestFloat(float, Enum): one = 1.0 two = 2.0 def __format__(self, spec): return 'TestFloat success!' + self.assertEqual(str(TestFloat.one), 'TestFloat.one') self.assertEqual('{}'.format(TestFloat.one), 'TestFloat success!') def assertFormatIsValue(self, spec, member): |