Skip to content
Merged
Show file tree
Hide file tree
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
wip
  • Loading branch information
ethanfurman committed Mar 25, 2021
commit db0386fbccc6974a1c3b1df7a442eac1fe6360a7
17 changes: 9 additions & 8 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
__all__ = [
'EnumMeta',
'Enum', 'IntEnum', 'StrEnum', 'Flag', 'IntFlag',
<<<<<<< HEAD
'auto', 'unique',
'property',
'property', 'global_flag_repr', 'global_int_repr',
'FlagBoundary', 'STRICT', 'CONFORM', 'EJECT', 'KEEP',
=======
'auto', 'unique', 'global_flag_repr', 'global_int_repr',
>>>>>>> add new repr and str for converted Int-Enums/Flags
]


Expand Down Expand Up @@ -784,7 +780,8 @@ def _convert_(cls, name, module, filter, source=None, boundary=None):
cls.__repr__ = global_flag_repr
elif issubclass(cls, int):
cls.__repr__ = global_int_repr
cls.__str__ = object.__str__
# if str not in cls.__mro__:
# cls.__str__ = global_enum_str
module_globals.update(cls.__members__)
module_globals[name] = cls
return cls
Expand Down Expand Up @@ -1338,6 +1335,10 @@ def _power_of_two(value):
if value < 1:
return False
return value == 2 ** _high_bit(value)

def global_enum_str(self):
return self.name

def global_int_repr(self):
return '%s.%s' % (self.__class__.__module__, self.name)

Expand All @@ -1352,7 +1353,7 @@ def global_flag_repr(self):
res = '|'.join(members)
if negative:
if len(members) > 1:
res = f'~({res})'
res = '~(%s)' % (res, )
else:
res = f'~{res}'
res = '~%s' % (res, )
return res
32 changes: 30 additions & 2 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3590,14 +3590,42 @@ def test_convert_raise(self):
filter=lambda x: x.startswith('CONVERT_TEST_'))

def test_convert_repr_and_str(self):

module = ('test.test_enum', '__main__')[__name__=='__main__']
test_type = enum.IntEnum._convert_(
'UnittestConvert',
module,
filter=lambda x: x.startswith('CONVERT_TEST_'))
self.assertEqual(repr(test_type.CONVERT_TEST_NAME_A), '%s.CONVERT_TEST_NAME_A' % module)
self.assertEqual(str(test_type.CONVERT_TEST_NAME_A), '%s.CONVERT_TEST_NAME_A' % module)
self.assertEqual(str(test_type.CONVERT_TEST_NAME_A), 'CONVERT_TEST_NAME_A')

# global names for StrEnum._convert_ test
CONVERT_STR_TEST_2 = 'goodbye'
CONVERT_STR_TEST_1 = 'hello'

class TestStrEnumConvert(unittest.TestCase):

def test_convert(self):
test_type = enum.StrEnum._convert_(
'UnittestConvert',
('test.test_enum', '__main__')[__name__=='__main__'],
filter=lambda x: x.startswith('CONVERT_STR_'))
# Ensure that test_type has all of the desired names and values.
self.assertEqual(test_type.CONVERT_STR_TEST_1, 'hello')
self.assertEqual(test_type.CONVERT_STR_TEST_2, 'goodbye')
# Ensure that test_type only picked up names matching the filter.
self.assertEqual([name for name in dir(test_type)
if name[0:2] not in ('CO', '__')],
[], msg='Names other than CONVERT_STR_* found.')

def test_convert_repr_and_str(self):
module = ('test.test_enum', '__main__')[__name__=='__main__']
test_type = enum.StrEnum._convert_(
'UnittestConvert',
module,
filter=lambda x: x.startswith('CONVERT_STR_'))
self.assertEqual(repr(test_type.CONVERT_STR_TEST_1), '%s.CONVERT_STR_TEST_1' % module)
self.assertEqual(str(test_type.CONVERT_STR_TEST_2), 'goodbye')


if __name__ == '__main__':
unittest.main()