Skip to content

Commit 3b57460

Browse files
committed
Move parts of Enum how-to to API docs
To avoid duplicate content in the Enum HOWTO and API documentation which is not automatically synced, the section about supported __dunder__ and _sunder names is moved from HOWTO to API docs. See also #136791
1 parent 918e3ba commit 3b57460

File tree

2 files changed

+30
-64
lines changed

2 files changed

+30
-64
lines changed

Doc/howto/enum.rst

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -967,69 +967,10 @@ want one of them to be the value::
967967
Finer Points
968968
^^^^^^^^^^^^
969969

970-
Supported ``__dunder__`` names
971-
""""""""""""""""""""""""""""""
970+
Supported ``__dunder__`` and ``_sunder_`` names
971+
"""""""""""""""""""""""""""""""""""""""""""""""
972972

973-
:attr:`~enum.EnumType.__members__` is a read-only ordered mapping of ``member_name``:``member``
974-
items. It is only available on the class.
975-
976-
:meth:`~object.__new__`, if specified, must create and return the enum members; it is
977-
also a very good idea to set the member's :attr:`~Enum._value_` appropriately. Once
978-
all the members are created it is no longer used.
979-
980-
981-
Supported ``_sunder_`` names
982-
""""""""""""""""""""""""""""
983-
984-
- :attr:`~Enum._name_` -- name of the member
985-
- :attr:`~Enum._value_` -- value of the member; can be set in ``__new__``
986-
- :meth:`~Enum._missing_` -- a lookup function used when a value is not found;
987-
may be overridden
988-
- :attr:`~Enum._ignore_` -- a list of names, either as a :class:`list` or a
989-
:class:`str`, that will not be transformed into members, and will be removed
990-
from the final class
991-
- :meth:`~Enum._generate_next_value_` -- used to get an appropriate value for
992-
an enum member; may be overridden
993-
- :meth:`~Enum._add_alias_` -- adds a new name as an alias to an existing
994-
member.
995-
- :meth:`~Enum._add_value_alias_` -- adds a new value as an alias to an
996-
existing member. See `MultiValueEnum`_ for an example.
997-
998-
.. note::
999-
1000-
For standard :class:`Enum` classes the next value chosen is the highest
1001-
value seen incremented by one.
1002-
1003-
For :class:`Flag` classes the next value chosen will be the next highest
1004-
power-of-two.
1005-
1006-
.. versionchanged:: 3.13
1007-
Prior versions would use the last seen value instead of the highest value.
1008-
1009-
.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_``
1010-
.. versionadded:: 3.7 ``_ignore_``
1011-
.. versionadded:: 3.13 ``_add_alias_``, ``_add_value_alias_``
1012-
1013-
To help keep Python 2 / Python 3 code in sync an :attr:`~Enum._order_` attribute can
1014-
be provided. It will be checked against the actual order of the enumeration
1015-
and raise an error if the two do not match::
1016-
1017-
>>> class Color(Enum):
1018-
... _order_ = 'RED GREEN BLUE'
1019-
... RED = 1
1020-
... BLUE = 3
1021-
... GREEN = 2
1022-
...
1023-
Traceback (most recent call last):
1024-
...
1025-
TypeError: member order does not match _order_:
1026-
['RED', 'BLUE', 'GREEN']
1027-
['RED', 'GREEN', 'BLUE']
1028-
1029-
.. note::
1030-
1031-
In Python 2 code the :attr:`~Enum._order_` attribute is necessary as definition
1032-
order is lost before it can be recorded.
973+
The supported ``__dunder__`` and ``_sunder_`` names can be found in the :ref:`Enum API documentation <enum-dunder-sunder>`.
1033974

1034975

1035976
_Private__names
@@ -1478,6 +1419,7 @@ alias::
14781419
behaviors as well as disallowing aliases. If the only desired change is
14791420
disallowing aliases, the :func:`unique` decorator can be used instead.
14801421

1422+
.. _multi-value-enum:
14811423

14821424
MultiValueEnum
14831425
^^^^^^^^^^^^^^^^^

Doc/library/enum.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,26 @@ Data Types
301301
No longer used, kept for backward compatibility.
302302
(class attribute, removed during class creation).
303303

304+
The :attr:`~Enum._order_` attribute can be provided to help keep Python 2 / Python 3 code in sync.
305+
It will be checked against the actual order of the enumeration and raise an error if the two do not match::
306+
307+
>>> class Color(Enum):
308+
... _order_ = 'RED GREEN BLUE'
309+
... RED = 1
310+
... BLUE = 3
311+
... GREEN = 2
312+
...
313+
Traceback (most recent call last):
314+
...
315+
TypeError: member order does not match _order_:
316+
['RED', 'BLUE', 'GREEN']
317+
['RED', 'GREEN', 'BLUE']
318+
319+
.. note::
320+
321+
In Python 2 code the :attr:`~Enum._order_` attribute is necessary as definition
322+
order is lost before it can be recorded.
323+
304324
.. attribute:: Enum._ignore_
305325

306326
``_ignore_`` is only used during creation and is removed from the
@@ -480,7 +500,8 @@ Data Types
480500
>>> Color(42)
481501
<Color.RED: 1>
482502

483-
Raises a :exc:`ValueError` if the value is already linked with a different member.
503+
| Raises a :exc:`ValueError` if the value is already linked with a different member.
504+
| See :ref:`multi-value-enum` for an example.
484505
485506
.. versionadded:: 3.13
486507

@@ -879,14 +900,16 @@ Data Types
879900

880901
---------------
881902

903+
.. _enum-dunder-sunder:
904+
882905
Supported ``__dunder__`` names
883906
""""""""""""""""""""""""""""""
884907

885908
:attr:`~EnumType.__members__` is a read-only ordered mapping of ``member_name``:``member``
886909
items. It is only available on the class.
887910

888911
:meth:`~Enum.__new__`, if specified, must create and return the enum members;
889-
it is also a very good idea to set the member's :attr:`!_value_` appropriately.
912+
it is also a very good idea to set the member's :attr:`~Enum._value_` appropriately.
890913
Once all the members are created it is no longer used.
891914

892915

@@ -902,6 +925,7 @@ Supported ``_sunder_`` names
902925
from the final class
903926
- :attr:`~Enum._order_` -- no longer used, kept for backward
904927
compatibility (class attribute, removed during class creation)
928+
905929
- :meth:`~Enum._generate_next_value_` -- used to get an appropriate value for
906930
an enum member; may be overridden
907931

0 commit comments

Comments
 (0)