|
14 | 14 | from enum import Enum, EnumMeta, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto |
15 | 15 | from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum |
16 | 16 | from enum import verify, UNIQUE, CONTINUOUS, NAMED_FLAGS, ReprEnum |
17 | | -from enum import member, nonmember, _iter_bits_lsb |
| 17 | +from enum import member, nonmember, _iter_bits_lsb, EnumDict |
18 | 18 | from io import StringIO |
19 | 19 | from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL |
20 | 20 | from test import support |
@@ -5414,6 +5414,37 @@ def test_convert_repr_and_str(self): |
5414 | 5414 | self.assertEqual(format(test_type.CONVERT_STRING_TEST_NAME_A), '5') |
5415 | 5415 |
|
5416 | 5416 |
|
| 5417 | +class TestEnumDict(unittest.TestCase): |
| 5418 | + def test_enum_dict_in_metaclass(self): |
| 5419 | + """Test that EnumDict is usable as a class namespace""" |
| 5420 | + class Meta(type): |
| 5421 | + @classmethod |
| 5422 | + def __prepare__(metacls, cls, bases, **kwds): |
| 5423 | + return EnumDict() |
| 5424 | + |
| 5425 | + class MyClass(metaclass=Meta): |
| 5426 | + a = 1 |
| 5427 | + |
| 5428 | + with self.assertRaises(TypeError): |
| 5429 | + a = 2 # duplicate |
| 5430 | + |
| 5431 | + with self.assertRaises(ValueError): |
| 5432 | + _a_sunder_ = 3 |
| 5433 | + |
| 5434 | + def test_enum_dict_standalone(self): |
| 5435 | + """Test that EnumDict is usable on its own""" |
| 5436 | + enumdict = EnumDict() |
| 5437 | + enumdict['a'] = 1 |
| 5438 | + |
| 5439 | + with self.assertRaises(TypeError): |
| 5440 | + enumdict['a'] = 'other value' |
| 5441 | + |
| 5442 | + # Only MutableMapping interface is overridden for now. |
| 5443 | + # If this starts passing, update the documentation. |
| 5444 | + enumdict |= {'a': 'other value'} |
| 5445 | + self.assertEqual(enumdict['a'], 'other value') |
| 5446 | + |
| 5447 | + |
5417 | 5448 | # helpers |
5418 | 5449 |
|
5419 | 5450 | def enum_dir(cls): |
|
0 commit comments