Skip to content

gh-98298: [Enum] document ReprEnum, global_enum, and show_flag_values #98455

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 4 commits into from
Oct 21, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Doc/library/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ Module Contents
the bitwise operators without losing their :class:`IntFlag` membership.
:class:`IntFlag` members are also subclasses of :class:`int`. (`Notes`_)

:class:`ReprEnum`

Used by ``IntEnum``, ``StrEnum``, and ``IntFlag`` to keep the ``str()`` of
the mixed-in type.

:class:`EnumCheck`

An enumeration with the values ``CONTINUOUS``, ``NAMED_FLAGS``, and
Expand Down Expand Up @@ -132,9 +137,19 @@ Module Contents

Do not make ``obj`` a member. Can be used as a decorator.

:func:`global_enum`

Modify the ``str()`` and ``repr()`` of an enum to show its module instead of
its class -- should only be used if the enum members will be exported to the
global namespace.

:func:`show_flag_values`

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


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

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

Expand Down Expand Up @@ -573,6 +588,17 @@ Data Types
better support the *replacement of existing constants* use-case.
:meth:`__format__` was already :func:`int.__format__` for that same reason.

.. class:: ReprEnum

*ReprEum* uses the *repr()* of *Enum*, but the *str()* of the mixed-in data type:

* :meth:`int.__str__` for *IntEnum* and *IntFlag*
* :meth:`str.__str__` for *StrEnum*

Inherit from *ReprEnum* when mixing in a data type when the *str()* output should
not be changed to the *Enum*-default *str()*.

.. versionadded:: 3.11

.. class:: EnumCheck

Expand Down Expand Up @@ -808,6 +834,20 @@ Utilities and Decorators

.. versionadded:: 3.11

.. decorator:: global_enum

A decorator to change the *str()* and *repr()* of an enum to show the module
of the enum instead of its class. Should only be used when the enum members
are exported to the global namespace (see *re.RegexFlag* for an example).

.. versionadded:: 3.11

.. function:: show_flag_values

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

.. versionadded:: 3.11

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

Notes
Expand Down