mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.13] gh-112328: Make EnumDict usable on its own and document it (GH-123669) (GH-128142)
Co-authored-by: Petr Viktorin <pviktori@redhat.com>
This commit is contained in:
parent
a52d663d0c
commit
875e49fb63
5 changed files with 72 additions and 8 deletions
|
@ -15,7 +15,7 @@ from functools import partial
|
|||
from enum import Enum, EnumMeta, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto
|
||||
from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum
|
||||
from enum import verify, UNIQUE, CONTINUOUS, NAMED_FLAGS, ReprEnum
|
||||
from enum import member, nonmember, _iter_bits_lsb
|
||||
from enum import member, nonmember, _iter_bits_lsb, EnumDict
|
||||
from io import StringIO
|
||||
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
|
||||
from test import support
|
||||
|
@ -5454,6 +5454,37 @@ class TestConvert(unittest.TestCase):
|
|||
self.assertEqual(format(test_type.CONVERT_STRING_TEST_NAME_A), '5')
|
||||
|
||||
|
||||
class TestEnumDict(unittest.TestCase):
|
||||
def test_enum_dict_in_metaclass(self):
|
||||
"""Test that EnumDict is usable as a class namespace"""
|
||||
class Meta(type):
|
||||
@classmethod
|
||||
def __prepare__(metacls, cls, bases, **kwds):
|
||||
return EnumDict(cls)
|
||||
|
||||
class MyClass(metaclass=Meta):
|
||||
a = 1
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
a = 2 # duplicate
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
_a_sunder_ = 3
|
||||
|
||||
def test_enum_dict_standalone(self):
|
||||
"""Test that EnumDict is usable on its own"""
|
||||
enumdict = EnumDict()
|
||||
enumdict['a'] = 1
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
enumdict['a'] = 'other value'
|
||||
|
||||
# Only MutableMapping interface is overridden for now.
|
||||
# If this stops passing, update the documentation.
|
||||
enumdict |= {'a': 'other value'}
|
||||
self.assertEqual(enumdict['a'], 'other value')
|
||||
|
||||
|
||||
# helpers
|
||||
|
||||
def enum_dir(cls):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue