mirror of
https://github.com/python/cpython.git
synced 2025-10-21 22:22:48 +00:00
gh-112328: [Enum] Make some private attributes public. (GH-112514)
* [Enum] Make some private attributes public. - ``_EnumDict`` --> ``EnumDict`` - ``EnumDict._member_names`` --> ``EnumDict.member_names`` - ``Enum._add_alias_`` - ``Enum._add_value_alias_`` --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
563ccded6e
commit
de6bca9564
5 changed files with 301 additions and 117 deletions
|
@ -514,6 +514,7 @@ class _EnumTests:
|
|||
self.assertFalse('first' in MainEnum)
|
||||
val = MainEnum.dupe
|
||||
self.assertIn(val, MainEnum)
|
||||
self.assertNotIn(float('nan'), MainEnum)
|
||||
#
|
||||
class OtherEnum(Enum):
|
||||
one = auto()
|
||||
|
@ -3268,6 +3269,65 @@ class TestSpecial(unittest.TestCase):
|
|||
member._value_ = Base(value)
|
||||
return member
|
||||
|
||||
def test_extra_member_creation(self):
|
||||
class IDEnumMeta(EnumMeta):
|
||||
def __new__(metacls, cls, bases, classdict, **kwds):
|
||||
# add new entries to classdict
|
||||
for name in classdict.member_names:
|
||||
classdict[f'{name}_DESC'] = f'-{classdict[name]}'
|
||||
return super().__new__(metacls, cls, bases, classdict, **kwds)
|
||||
class IDEnum(StrEnum, metaclass=IDEnumMeta):
|
||||
pass
|
||||
class MyEnum(IDEnum):
|
||||
ID = 'id'
|
||||
NAME = 'name'
|
||||
self.assertEqual(list(MyEnum), [MyEnum.ID, MyEnum.NAME, MyEnum.ID_DESC, MyEnum.NAME_DESC])
|
||||
|
||||
def test_add_alias(self):
|
||||
class mixin:
|
||||
@property
|
||||
def ORG(self):
|
||||
return 'huh'
|
||||
class Color(mixin, Enum):
|
||||
RED = 1
|
||||
GREEN = 2
|
||||
BLUE = 3
|
||||
Color.RED._add_alias_('ROJO')
|
||||
self.assertIs(Color.RED, Color['ROJO'])
|
||||
self.assertIs(Color.RED, Color.ROJO)
|
||||
Color.BLUE._add_alias_('ORG')
|
||||
self.assertIs(Color.BLUE, Color['ORG'])
|
||||
self.assertIs(Color.BLUE, Color.ORG)
|
||||
self.assertEqual(Color.RED.ORG, 'huh')
|
||||
self.assertEqual(Color.GREEN.ORG, 'huh')
|
||||
self.assertEqual(Color.BLUE.ORG, 'huh')
|
||||
self.assertEqual(Color.ORG.ORG, 'huh')
|
||||
|
||||
def test_add_value_alias_after_creation(self):
|
||||
class Color(Enum):
|
||||
RED = 1
|
||||
GREEN = 2
|
||||
BLUE = 3
|
||||
Color.RED._add_value_alias_(5)
|
||||
self.assertIs(Color.RED, Color(5))
|
||||
|
||||
def test_add_value_alias_during_creation(self):
|
||||
class Types(Enum):
|
||||
Unknown = 0,
|
||||
Source = 1, 'src'
|
||||
NetList = 2, 'nl'
|
||||
def __new__(cls, int_value, *value_aliases):
|
||||
member = object.__new__(cls)
|
||||
member._value_ = int_value
|
||||
for alias in value_aliases:
|
||||
member._add_value_alias_(alias)
|
||||
return member
|
||||
self.assertIs(Types(0), Types.Unknown)
|
||||
self.assertIs(Types(1), Types.Source)
|
||||
self.assertIs(Types('src'), Types.Source)
|
||||
self.assertIs(Types(2), Types.NetList)
|
||||
self.assertIs(Types('nl'), Types.NetList)
|
||||
|
||||
|
||||
class TestOrder(unittest.TestCase):
|
||||
"test usage of the `_order_` attribute"
|
||||
|
@ -4941,12 +5001,14 @@ class TestStdLib(unittest.TestCase):
|
|||
@bltns.property
|
||||
def zeroth(self):
|
||||
return 'zeroed %s' % self.name
|
||||
self.assertTrue(_test_simple_enum(CheckedColor, SimpleColor) is None)
|
||||
_test_simple_enum(CheckedColor, SimpleColor)
|
||||
SimpleColor.MAGENTA._value_ = 9
|
||||
self.assertRaisesRegex(
|
||||
TypeError, "enum mismatch",
|
||||
_test_simple_enum, CheckedColor, SimpleColor,
|
||||
)
|
||||
#
|
||||
#
|
||||
class CheckedMissing(IntFlag, boundary=KEEP):
|
||||
SIXTY_FOUR = 64
|
||||
ONE_TWENTY_EIGHT = 128
|
||||
|
@ -4963,8 +5025,28 @@ class TestStdLib(unittest.TestCase):
|
|||
ALL = 2048 + 128 + 64 + 12
|
||||
M = Missing
|
||||
self.assertEqual(list(CheckedMissing), [M.SIXTY_FOUR, M.ONE_TWENTY_EIGHT, M.TWENTY_FORTY_EIGHT])
|
||||
#
|
||||
_test_simple_enum(CheckedMissing, Missing)
|
||||
#
|
||||
#
|
||||
class CheckedUnhashable(Enum):
|
||||
ONE = dict()
|
||||
TWO = set()
|
||||
name = 'python'
|
||||
self.assertIn(dict(), CheckedUnhashable)
|
||||
self.assertIn('python', CheckedUnhashable)
|
||||
self.assertEqual(CheckedUnhashable.name.value, 'python')
|
||||
self.assertEqual(CheckedUnhashable.name.name, 'name')
|
||||
#
|
||||
@_simple_enum()
|
||||
class Unhashable:
|
||||
ONE = dict()
|
||||
TWO = set()
|
||||
name = 'python'
|
||||
self.assertIn(dict(), Unhashable)
|
||||
self.assertIn('python', Unhashable)
|
||||
self.assertEqual(Unhashable.name.value, 'python')
|
||||
self.assertEqual(Unhashable.name.name, 'name')
|
||||
_test_simple_enum(Unhashable, Unhashable)
|
||||
|
||||
|
||||
class MiscTestCase(unittest.TestCase):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue