[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:
Ethan Furman 2024-12-24 10:50:23 -08:00 committed by GitHub
parent a52d663d0c
commit 875e49fb63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 72 additions and 8 deletions

View file

@ -343,12 +343,13 @@ class EnumDict(dict):
EnumType will use the names found in self._member_names as the
enumeration member names.
"""
def __init__(self):
def __init__(self, cls_name=None):
super().__init__()
self._member_names = {} # use a dict -- faster look-up than a list, and keeps insertion order since 3.7
self._last_values = []
self._ignore = []
self._auto_called = False
self._cls_name = cls_name
def __setitem__(self, key, value):
"""
@ -359,7 +360,7 @@ def __setitem__(self, key, value):
Single underscore (sunder) names are reserved.
"""
if _is_private(self._cls_name, key):
if self._cls_name is not None and _is_private(self._cls_name, key):
# do nothing, name will be a normal attribute
pass
elif _is_sunder(key):
@ -413,7 +414,7 @@ def __setitem__(self, key, value):
'old behavior', FutureWarning, stacklevel=2)
elif _is_descriptor(value):
pass
elif _is_internal_class(self._cls_name, value):
elif self._cls_name is not None and _is_internal_class(self._cls_name, value):
# do nothing, name will be a normal attribute
pass
else:
@ -485,8 +486,7 @@ def __prepare__(metacls, cls, bases, **kwds):
# check that previous enum members do not exist
metacls._check_for_existing_members_(cls, bases)
# create the namespace dict
enum_dict = EnumDict()
enum_dict._cls_name = cls
enum_dict = EnumDict(cls)
# inherit previous flags and _generate_next_value_ function
member_type, first_enum = metacls._get_mixins_(cls, bases)
if first_enum is not None: