8000 bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__ by jason-curtis · Pull Request #14545 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37479: on Enum subclasses with mixins, __format__ uses overridden __str__ #14545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 4, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More complete test coverage
  • Loading branch information
jason-curtis committed Jul 2, 2019
commit f46f951c8c48ad56ce2e478b0c5773c9dc1f03eb
46 changes: 39 additions & 7 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,54 @@ def test_format_enum(self):
self.assertEqual('{:<20}'.format(Season.SPRING),
'{:<20}'.format(str(Season.SPRING)))

def test_str_enum_custom(self):
class TestIntWithStrOverride(IntEnum):
one = 1
two = 2
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_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!')

self.assertEqual(str(TestIntWithStrOverride.one), 'Overridden!')
self.assertEqual('{}'.format(TestIntWithStrOverride.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_enum_custom(self):
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):
Expand Down
0