Skip to content

Commit 003bd8c

Browse files
gh-136672: Docs: Move Enum functions and add examples (GH-136791)
* Docs: Move Enum functions and add examples When the `Enum` functions `_add_alias_` and `_add_value_alias_` were added in de6bca9, the documentation for them was done under `EnumType` instead of `Enum`. This change moves them to the docs of the `Enum` class and adds an example for each function. --------- Co-authored-by: Ethan Furman <[email protected]>
1 parent 654b8d9 commit 003bd8c

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Doc/howto/enum.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,9 @@ Supported ``_sunder_`` names
990990
from the final class
991991
- :meth:`~Enum._generate_next_value_` -- used to get an appropriate value for
992992
an enum member; may be overridden
993-
- :meth:`~EnumType._add_alias_` -- adds a new name as an alias to an existing
993+
- :meth:`~Enum._add_alias_` -- adds a new name as an alias to an existing
994994
member.
995-
- :meth:`~EnumType._add_value_alias_` -- adds a new value as an alias to an
995+
- :meth:`~Enum._add_value_alias_` -- adds a new value as an alias to an
996996
existing member. See `MultiValueEnum`_ for an example.
997997

998998
.. note::

Doc/library/enum.rst

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ Data Types
175175
final *enum*, as well as creating the enum members, properly handling
176176
duplicates, providing iteration over the enum class, etc.
177177

178+
.. versionadded:: 3.11
179+
180+
Before 3.11 ``EnumType`` was called ``EnumMeta``, which is still available as an alias.
181+
178182
.. method:: EnumType.__call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
179183

180184
This method is called in two different ways:
@@ -206,7 +210,7 @@ Data Types
206210
>>> Color.RED.value in Color
207211
True
208212

209-
.. versionchanged:: 3.12
213+
.. versionchanged:: 3.12
210214

211215
Before Python 3.12, a ``TypeError`` is raised if a
212216
non-Enum-member is used in a containment check.
@@ -251,20 +255,6 @@ Data Types
251255
>>> list(reversed(Color))
252256
[<Color.BLUE: 3>, <Color.GREEN: 2>, <Color.RED: 1>]
253257

254-
.. method:: EnumType._add_alias_
255-
256-
Adds a new name as an alias to an existing member. Raises a
257-
:exc:`NameError` if the name is already assigned to a different member.
258-
259-
.. method:: EnumType._add_value_alias_
260-
261-
Adds a new value as an alias to an existing member. Raises a
262-
:exc:`ValueError` if the value is already linked with a different member.
263-
264-
.. versionadded:: 3.11
265-
266-
Before 3.11 ``EnumType`` was called ``EnumMeta``, which is still available as an alias.
267-
268258

269259
.. class:: Enum
270260

@@ -470,6 +460,30 @@ Data Types
470460

471461
.. versionchanged:: 3.12 Added :ref:`enum-dataclass-support`
472462

463+
.. method:: Enum._add_alias_
464+
465+
Adds a new name as an alias to an existing member::
466+
467+
>>> Color.RED._add_alias_("ERROR")
468+
>>> Color.ERROR
469+
<Color.RED: 1>
470+
471+
Raises a :exc:`NameError` if the name is already assigned to a different member.
472+
473+
.. versionadded:: 3.13
474+
475+
.. method:: Enum._add_value_alias_
476+
477+
Adds a new value as an alias to an existing member::
478+
479+
>>> Color.RED._add_value_alias_(42)
480+
>>> Color(42)
481+
<Color.RED: 1>
482+
483+
Raises a :exc:`ValueError` if the value is already linked with a different member.
484+
485+
.. versionadded:: 3.13
486+
473487

474488
.. class:: IntEnum
475489

@@ -879,10 +893,6 @@ Once all the members are created it is no longer used.
879893
Supported ``_sunder_`` names
880894
""""""""""""""""""""""""""""
881895

882-
- :meth:`~EnumType._add_alias_` -- adds a new name as an alias to an existing
883-
member.
884-
- :meth:`~EnumType._add_value_alias_` -- adds a new value as an alias to an
885-
existing member.
886896
- :attr:`~Enum._name_` -- name of the member
887897
- :attr:`~Enum._value_` -- value of the member; can be set in ``__new__``
888898
- :meth:`~Enum._missing_` -- a lookup function used when a value is not found;
@@ -903,6 +913,11 @@ Supported ``_sunder_`` names
903913
For :class:`Flag` classes the next value chosen will be the next highest
904914
power-of-two.
905915

916+
- :meth:`~Enum._add_alias_` -- adds a new name as an alias to an existing
917+
member.
918+
- :meth:`~Enum._add_value_alias_` -- adds a new value as an alias to an
919+
existing member.
920+
906921
- While ``_sunder_`` names are generally reserved for the further development
907922
of the :class:`Enum` class and can not be used, some are explicitly allowed:
908923

0 commit comments

Comments
 (0)