Skip to content

Commit 694d8f7

Browse files
committed
updates
1 parent a33aa0c commit 694d8f7

File tree

5 files changed

+13
-26
lines changed

5 files changed

+13
-26
lines changed

Doc/library/enum.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ Module Contents
112112

113113
:class:`EnumDict`
114114

115-
A subclass of :class:`dict` that tracks order and enforces unique
116-
member names.
115+
A subclass of :class:`dict` for use when subclassing :class:`EnumType`.
117116

118117
:class:`auto`
119118

@@ -154,14 +153,10 @@ Module Contents
154153

155154
Return a list of all power-of-two integers contained in a flag.
156155

157-
:class:`EnumDict`
158-
159-
A subclass of :class:`dict` for use when subclassing :class:`EnumType`.
160-
161156

162157
.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
163158
.. versionadded:: 3.11 ``StrEnum``, ``EnumCheck``, ``ReprEnum``, ``FlagBoundary``, ``property``, ``member``, ``nonmember``, ``global_enum``, ``show_flag_values``
164-
.. versionadded:: 3.14 ``EnumDict``
159+
.. versionadded:: 3.13 ``EnumDict``
165160

166161
---------------
167162

Doc/whatsnew/3.13.rst

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -879,18 +879,12 @@ email
879879
(Contributed by Thomas Dwyer and Victor Stinner for :gh:`102988` to improve
880880
the :cve:`2023-27043` fix.)
881881

882-
enum
883-
----
884-
885-
* :class:`~enum.EnumDict` has been made public in :mod:`enum` to better support
886-
subclassing :class:`~enum.EnumType`.
887882

888883
enum
889884
----
890885

891-
* :class:`enum.EnumDict` has been made public, in order to allow subclasses
892-
of :class:`~enum.EnumType` with advanced behavior like having multiple values
893-
per member.
886+
* :class:`~enum.EnumDict` has been made public to better support subclassing
887+
:class:`~enum.EnumType`.
894888

895889

896890
fractions

Lib/enum.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,13 @@ class EnumDict(dict):
342342
EnumType will use the names found in self._member_names as the
343343
enumeration member names.
344344
"""
345-
def __init__(self):
345+
def __init__(self, cls_name):
346346
super().__init__()
347347
self._member_names = {} # use a dict -- faster look-up than a list, and keeps insertion order since 3.7
348348
self._last_values = []
349349
self._ignore = []
350350
self._auto_called = False
351-
self._cls_name = None
351+
self._cls_name = cls_name
352352

353353
def __setitem__(self, key, value):
354354
"""
@@ -359,7 +359,7 @@ def __setitem__(self, key, value):
359359
360360
Single underscore (sunder) names are reserved.
361361
"""
362-
if self._cls_name is not None and _is_private(self._cls_name, key):
362+
if _is_private(self._cls_name, key):
363363
# do nothing, name will be a normal attribute
364364
pass
365365
elif _is_sunder(key):
@@ -407,7 +407,7 @@ def __setitem__(self, key, value):
407407
value = value.value
408408
elif _is_descriptor(value):
409409
pass
410-
elif self._cls_name is not None and _is_internal_class(self._cls_name, value):
410+
elif _is_internal_class(self._cls_name, value):
411411
# do nothing, name will be a normal attribute
412412
pass
413413
else:
@@ -479,8 +479,7 @@ def __prepare__(metacls, cls, bases, **kwds):
479479
# check that previous enum members do not exist
480480
metacls._check_for_existing_members_(cls, bases)
481481
# create the namespace dict
482-
enum_dict = EnumDict()
483-
enum_dict._cls_name = cls
482+
enum_dict = EnumDict(cls)
484483
# inherit previous flags and _generate_next_value_ function
485484
member_type, first_enum = metacls._get_mixins_(cls, bases)
486485
if first_enum is not None:

Lib/test/test_enum.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5446,7 +5446,7 @@ def test_enum_dict_in_metaclass(self):
54465446
class Meta(type):
54475447
@classmethod
54485448
def __prepare__(metacls, cls, bases, **kwds):
5449-
return EnumDict()
5449+
return EnumDict(cls)
54505450

54515451
class MyClass(metaclass=Meta):
54525452
a = 1
@@ -5459,14 +5459,14 @@ class MyClass(metaclass=Meta):
54595459

54605460
def test_enum_dict_standalone(self):
54615461
"""Test that EnumDict is usable on its own"""
5462-
enumdict = EnumDict()
5462+
enumdict = EnumDict('test')
54635463
enumdict['a'] = 1
54645464

54655465
with self.assertRaises(TypeError):
54665466
enumdict['a'] = 'other value'
54675467

54685468
# Only MutableMapping interface is overridden for now.
5469-
# If this starts passing, update the documentation.
5469+
# If this stops passing, update the documentation.
54705470
enumdict |= {'a': 'other value'}
54715471
self.assertEqual(enumdict['a'], 'other value')
54725472

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
:class:`enum.EnumDict` can now be used on its own, without resorting to
2-
private API.
1+
:class:`enum.EnumDict` can now be used without resorting to private API.

0 commit comments

Comments
 (0)