8000 gh-112328: Make EnumDict usable on its own and document it by encukou · Pull Request #123669 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

gh-112328: Make EnumDict usable on its own and document it #123669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 20, 2024
Merged
Prev Previous commit
Next Next commit
updates
  • Loading branch information
ethanfurman committed Dec 20, 2024
commit 694d8f73997c469a5c32453b8dd73df7e87c7f4b
9 changes: 2 additions & 7 deletions Doc/library/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ Module Contents

:class:`EnumDict`

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

:class:`auto`

Expand Down Expand Up @@ -154,14 +153,10 @@ Module Contents

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

:class:`EnumDict`

A subclass of :class:`dict` for use when subclassing :class:`EnumType`.


.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
.. versionadded:: 3.11 ``StrEnum``, ``EnumCheck``, ``ReprEnum``, ``FlagBoundary``, ``property``, ``member``, ``nonmember``, ``global_enum``, ``show_flag_values``
.. versionadded:: 3.14 ``EnumDict``
.. versionadded:: 3.13 ``EnumDict``

---------------

Expand Down
10 changes: 2 additions & 8 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -879,18 +879,12 @@ email
(Contributed by Thomas Dwyer and Victor Stinner for :gh:`102988` to improve
the :cve:`2023-27043` fix.)

enum
----

* :class:`~enum.EnumDict` has been made public in :mod:`enum` to better support
subclassing :class:`~enum.EnumType`.

enum
----

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


fractions
Expand Down
11 changes: 5 additions & 6 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,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):
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 = None
self._cls_name = cls_name

def __setitem__(self, key, value):
"""
Expand All @@ -359,7 +359,7 @@ def __setitem__(self, key, value):

Single underscore (sunder) names are reserved.
"""
if self._cls_name is not None and _is_private(self._cls_name, key):
if _is_private(self._cls_name, key):
# do nothing, name will be a normal attribute
pass
elif _is_sunder(key):
8000 Expand Down Expand Up @@ -407,7 +407,7 @@ def __setitem__(self, key, value):
value = value.value
elif _is_descriptor(value):
pass
elif self._cls_name is not None and _is_internal_class(self._cls_name, value):
elif _is_internal_class(self._cls_name, value):
# do nothing, name will be a normal attribute
pass
else:
Expand Down Expand Up @@ -479,8 +479,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:
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5446,7 +5446,7 @@ def test_enum_dict_in_metaclass(self):
class Meta(type):
@classmethod
def __prepare__(metacls, cls, bases, **kwds):
return EnumDict()
return EnumDict(cls)

class MyClass(metaclass=Meta):
a = 1
Expand All @@ -5459,14 +5459,14 @@ class MyClass(metaclass=Meta):

def test_enum_dict_standalone(self):
"""Test that EnumDict is usable on its own"""
enumdict = EnumDict()
enumdict = EnumDict('test')
enumdict['a'] = 1

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
:class:`enum.EnumDict` can now be used on its own, without resorting to
private API.
:class:`enum.EnumDict` can now be used without resorting to private API.
0