Skip to content

Commit 5ffc1e5

Browse files
authored
gh-98298, gh-74730: [Enum] update docs (GH-103163)
fix FlagBoundary statements add warning about reloading modules and enum identity
1 parent d3a7732 commit 5ffc1e5

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

Doc/howto/enum.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ below)::
372372
>>> Color.BLUE == 2
373373
False
374374

375+
.. warning::
376+
377+
It is possible to reload modules -- if a reloaded module contains
378+
enums, they will be recreated, and the new members may not
379+
compare identical/equal to the original members.
375380

376381
Allowed members and attributes of enumerations
377382
----------------------------------------------

Doc/library/enum.rst

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@ Module Contents
141141
:func:`global_enum`
142142

143143
Modify the :class:`str() <str>` and :func:`repr` of an enum
144-
to show its members as belonging to the module instead of its class.
145-
Should only be used if the enum members will be exported to the
146-
module global namespace.
144+
to show its members as belonging to the module instead of its class,
145+
and export the enum members to the global namespace.
147146

148147
:func:`show_flag_values`
149148

@@ -170,6 +169,27 @@ Data Types
170169
final *enum*, as well as creating the enum members, properly handling
171170
duplicates, providing iteration over the enum class, etc.
172171

172+
.. method:: EnumType.__call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
173+
174+
This method is called in two different ways:
175+
176+
* to look up an existing member:
177+
178+
:cls: The enum class being called.
179+
:value: The value to lookup.
180+
181+
* to use the ``cls`` enum to create a new enum (only if the existing enum
182+
does not have any members):
183+
184+
:cls: The enum class being called.
185+
:value: The name of the new Enum to create.
186+
:names: The names/values of the members for the new Enum.
187+
:module: The name of the module the new Enum is created in.
188+
:qualname: The actual location in the module where this Enum can be found.
189+
:type: A mix-in type for the new Enum.
190+
:start: The first integer value for the Enum (used by :class:`auto`).
191+
:boundary: How to handle out-of-range values from bit operations (:class:`Flag` only).
192+
173193
.. method:: EnumType.__contains__(cls, member)
174194

175195
Returns ``True`` if member belongs to the ``cls``::
@@ -255,26 +275,6 @@ Data Types
255275
names will also be removed from the completed enumeration. See
256276
:ref:`TimePeriod <enum-time-period>` for an example.
257277

258-
.. method:: Enum.__call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
259-
260-
This method is called in two different ways:
261-
262-
* to look up an existing member:
263-
264-
:cls: The enum class being called.
265-
:value: The value to lookup.
266-
267-
* to use the ``cls`` enum to create a new enum:
268-
269-
:cls: The enum class being called.
270-
:value: The name of the new Enum to create.
271-
:names: The names/values of the members for the new Enum.
272-
:module: The name of the module the new Enum is created in.
273-
:qualname: The actual location in the module where this Enum can be found.
274-
:type: A mix-in type for the new Enum.
275-
:start: The first integer value for the Enum (used by :class:`auto`).
276-
:boundary: How to handle out-of-range values from bit operations (:class:`Flag` only).
277-
278278
.. method:: Enum.__dir__(self)
279279

280280
Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and
@@ -728,7 +728,6 @@ Data Types
728728
.. attribute:: EJECT
729729

730730
Out-of-range values lose their *Flag* membership and revert to :class:`int`.
731-
This is the default for :class:`IntFlag`::
732731

733732
>>> from enum import Flag, EJECT, auto
734733
>>> class EjectFlag(Flag, boundary=EJECT):
@@ -741,8 +740,8 @@ Data Types
741740

742741
.. attribute:: KEEP
743742

744-
Out-of-range values are kept, and the *Flag* membership is kept. This is
745-
used for some stdlib flags::
743+
Out-of-range values are kept, and the *Flag* membership is kept.
744+
This is the default for :class:`IntFlag`::
746745

747746
>>> from enum import Flag, KEEP, auto
748747
>>> class KeepFlag(Flag, boundary=KEEP):

Lib/enum.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,10 +1301,10 @@ def _reduce_ex_by_global_name(self, proto):
13011301
class FlagBoundary(StrEnum):
13021302
"""
13031303
control how out of range values are handled
1304-
"strict" -> error is raised [default for Flag]
1305-
"conform" -> extra bits are discarded
1306-
"eject" -> lose flag status [default for IntFlag]
1307-
"keep" -> keep flag status and all bits
1304+
"strict" -> error is raised
1305+
"conform" -> extra bits are discarded [default for Flag]
1306+
"eject" -> lose flag status
1307+
"keep" -> keep flag status and all bits [default for IntFlag]
13081308
"""
13091309
STRICT = auto()
13101310
CONFORM = auto()

0 commit comments

Comments
 (0)