• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!enum` --- Support for enumerations
2=========================================
3
4.. module:: enum
5   :synopsis: Implementation of an enumeration class.
6
7.. moduleauthor:: Ethan Furman <ethan@stoneleaf.us>
8.. sectionauthor:: Barry Warsaw <barry@python.org>
9.. sectionauthor:: Eli Bendersky <eliben@gmail.com>
10.. sectionauthor:: Ethan Furman <ethan@stoneleaf.us>
11
12.. versionadded:: 3.4
13
14**Source code:** :source:`Lib/enum.py`
15
16.. sidebar:: Important
17
18   This page contains the API reference information. For tutorial
19   information and discussion of more advanced topics, see
20
21   * :ref:`Basic Tutorial <enum-basic-tutorial>`
22   * :ref:`Advanced Tutorial <enum-advanced-tutorial>`
23   * :ref:`Enum Cookbook <enum-cookbook>`
24
25---------------
26
27An enumeration:
28
29* is a set of symbolic names (members) bound to unique values
30* can be iterated over to return its canonical (i.e. non-alias) members in
31  definition order
32* uses *call* syntax to return members by value
33* uses *index* syntax to return members by name
34
35Enumerations are created either by using :keyword:`class` syntax, or by
36using function-call syntax::
37
38   >>> from enum import Enum
39
40   >>> # class syntax
41   >>> class Color(Enum):
42   ...     RED = 1
43   ...     GREEN = 2
44   ...     BLUE = 3
45
46   >>> # functional syntax
47   >>> Color = Enum('Color', [('RED', 1), ('GREEN', 2), ('BLUE', 3)])
48
49Even though we can use :keyword:`class` syntax to create Enums, Enums
50are not normal Python classes.  See
51:ref:`How are Enums different? <enum-class-differences>` for more details.
52
53.. note:: Nomenclature
54
55   - The class :class:`!Color` is an *enumeration* (or *enum*)
56   - The attributes :attr:`!Color.RED`, :attr:`!Color.GREEN`, etc., are
57     *enumeration members* (or *members*) and are functionally constants.
58   - The enum members have *names* and *values* (the name of
59     :attr:`!Color.RED` is ``RED``, the value of :attr:`!Color.BLUE` is
60     ``3``, etc.)
61
62---------------
63
64Module Contents
65---------------
66
67   :class:`EnumType`
68
69      The ``type`` for Enum and its subclasses.
70
71   :class:`Enum`
72
73      Base class for creating enumerated constants.
74
75   :class:`IntEnum`
76
77      Base class for creating enumerated constants that are also
78      subclasses of :class:`int`. (`Notes`_)
79
80   :class:`StrEnum`
81
82      Base class for creating enumerated constants that are also
83      subclasses of :class:`str`. (`Notes`_)
84
85   :class:`Flag`
86
87      Base class for creating enumerated constants that can be combined using
88      the bitwise operations without losing their :class:`Flag` membership.
89
90   :class:`IntFlag`
91
92      Base class for creating enumerated constants that can be combined using
93      the bitwise operators without losing their :class:`IntFlag` membership.
94      :class:`IntFlag` members are also subclasses of :class:`int`. (`Notes`_)
95
96   :class:`ReprEnum`
97
98      Used by :class:`IntEnum`, :class:`StrEnum`, and :class:`IntFlag`
99      to keep the :class:`str() <str>` of the mixed-in type.
100
101   :class:`EnumCheck`
102
103      An enumeration with the values ``CONTINUOUS``, ``NAMED_FLAGS``, and
104      ``UNIQUE``, for use with :func:`verify` to ensure various constraints
105      are met by a given enumeration.
106
107   :class:`FlagBoundary`
108
109      An enumeration with the values ``STRICT``, ``CONFORM``, ``EJECT``, and
110      ``KEEP`` which allows for more fine-grained control over how invalid values
111      are dealt with in an enumeration.
112
113   :class:`auto`
114
115      Instances are replaced with an appropriate value for Enum members.
116      :class:`StrEnum` defaults to the lower-cased version of the member name,
117      while other Enums default to 1 and increase from there.
118
119   :func:`~enum.property`
120
121      Allows :class:`Enum` members to have attributes without conflicting with
122      member names.  The ``value`` and ``name`` attributes are implemented this
123      way.
124
125   :func:`unique`
126
127      Enum class decorator that ensures only one name is bound to any one value.
128
129   :func:`verify`
130
131      Enum class decorator that checks user-selectable constraints on an
132      enumeration.
133
134   :func:`member`
135
136      Make ``obj`` a member.  Can be used as a decorator.
137
138   :func:`nonmember`
139
140      Do not make ``obj`` a member.  Can be used as a decorator.
141
142   :func:`global_enum`
143
144      Modify the :class:`str() <str>` and :func:`repr` of an enum
145      to show its members as belonging to the module instead of its class,
146      and export the enum members to the global namespace.
147
148   :func:`show_flag_values`
149
150      Return a list of all power-of-two integers contained in a flag.
151
152
153.. versionadded:: 3.6  ``Flag``, ``IntFlag``, ``auto``
154.. versionadded:: 3.11  ``StrEnum``, ``EnumCheck``, ``ReprEnum``, ``FlagBoundary``, ``property``, ``member``, ``nonmember``, ``global_enum``, ``show_flag_values``
155
156---------------
157
158Data Types
159----------
160
161
162.. class:: EnumType
163
164   *EnumType* is the :term:`metaclass` for *enum* enumerations.  It is possible
165   to subclass *EnumType* -- see :ref:`Subclassing EnumType <enumtype-examples>`
166   for details.
167
168   ``EnumType`` is responsible for setting the correct :meth:`!__repr__`,
169   :meth:`!__str__`, :meth:`!__format__`, and :meth:`!__reduce__` methods on the
170   final *enum*, as well as creating the enum members, properly handling
171   duplicates, providing iteration over the enum class, etc.
172
173   .. method:: EnumType.__call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
174
175      This method is called in two different ways:
176
177      * to look up an existing member:
178
179         :cls:   The enum class being called.
180         :value: The value to lookup.
181
182      * to use the ``cls`` enum to create a new enum (only if the existing enum
183        does not have any members):
184
185         :cls:   The enum class being called.
186         :value: The name of the new Enum to create.
187         :names: The names/values of the members for the new Enum.
188         :module:    The name of the module the new Enum is created in.
189         :qualname:  The actual location in the module where this Enum can be found.
190         :type:  A mix-in type for the new Enum.
191         :start: The first integer value for the Enum (used by :class:`auto`).
192         :boundary:  How to handle out-of-range values from bit operations (:class:`Flag` only).
193
194   .. method:: EnumType.__contains__(cls, member)
195
196      Returns ``True`` if member belongs to the ``cls``::
197
198        >>> some_var = Color.RED
199        >>> some_var in Color
200        True
201        >>> Color.RED.value in Color
202        True
203
204   .. versionchanged:: 3.12
205
206         Before Python 3.12, a ``TypeError`` is raised if a
207         non-Enum-member is used in a containment check.
208
209   .. method:: EnumType.__dir__(cls)
210
211      Returns ``['__class__', '__doc__', '__members__', '__module__']`` and the
212      names of the members in *cls*::
213
214        >>> dir(Color)
215        ['BLUE', 'GREEN', 'RED', '__class__', '__contains__', '__doc__', '__getitem__', '__init_subclass__', '__iter__', '__len__', '__members__', '__module__', '__name__', '__qualname__']
216
217   .. method:: EnumType.__getitem__(cls, name)
218
219      Returns the Enum member in *cls* matching *name*, or raises a :exc:`KeyError`::
220
221        >>> Color['BLUE']
222        <Color.BLUE: 3>
223
224   .. method:: EnumType.__iter__(cls)
225
226      Returns each member in *cls* in definition order::
227
228        >>> list(Color)
229        [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>]
230
231   .. method:: EnumType.__len__(cls)
232
233      Returns the number of member in *cls*::
234
235        >>> len(Color)
236        3
237
238   .. attribute:: EnumType.__members__
239
240      Returns a mapping of every enum name to its member, including aliases
241
242   .. method:: EnumType.__reversed__(cls)
243
244      Returns each member in *cls* in reverse definition order::
245
246        >>> list(reversed(Color))
247        [<Color.BLUE: 3>, <Color.GREEN: 2>, <Color.RED: 1>]
248
249   .. method:: EnumType._add_alias_
250
251      Adds a new name as an alias to an existing member.  Raises a
252      :exc:`NameError` if the name is already assigned to a different member.
253
254   .. method:: EnumType._add_value_alias_
255
256      Adds a new value as an alias to an existing member.  Raises a
257      :exc:`ValueError` if the value is already linked with a different member.
258
259   .. versionadded:: 3.11
260
261      Before 3.11 ``EnumType`` was called ``EnumMeta``, which is still available as an alias.
262
263
264.. class:: Enum
265
266   *Enum* is the base class for all *enum* enumerations.
267
268   .. attribute:: Enum.name
269
270      The name used to define the ``Enum`` member::
271
272        >>> Color.BLUE.name
273        'BLUE'
274
275   .. attribute:: Enum.value
276
277      The value given to the ``Enum`` member::
278
279         >>> Color.RED.value
280         1
281
282      Value of the member, can be set in :meth:`~Enum.__new__`.
283
284      .. note:: Enum member values
285
286         Member values can be anything: :class:`int`, :class:`str`, etc.  If
287         the exact value is unimportant you may use :class:`auto` instances and an
288         appropriate value will be chosen for you.  See :class:`auto` for the
289         details.
290
291         While mutable/unhashable values, such as :class:`dict`, :class:`list` or
292         a mutable :class:`~dataclasses.dataclass`, can be used, they will have a
293         quadratic performance impact during creation relative to the
294         total number of mutable/unhashable values in the enum.
295
296   .. attribute:: Enum._name_
297
298      Name of the member.
299
300   .. attribute:: Enum._value_
301
302      Value of the member, can be set in :meth:`~Enum.__new__`.
303
304   .. attribute:: Enum._order_
305
306      No longer used, kept for backward compatibility.
307      (class attribute, removed during class creation).
308
309   .. attribute:: Enum._ignore_
310
311      ``_ignore_`` is only used during creation and is removed from the
312      enumeration once creation is complete.
313
314      ``_ignore_`` is a list of names that will not become members, and whose
315      names will also be removed from the completed enumeration.  See
316      :ref:`TimePeriod <enum-time-period>` for an example.
317
318   .. method:: Enum.__dir__(self)
319
320      Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and
321      any public methods defined on *self.__class__*::
322
323         >>> from datetime import date
324         >>> class Weekday(Enum):
325         ...     MONDAY = 1
326         ...     TUESDAY = 2
327         ...     WEDNESDAY = 3
328         ...     THURSDAY = 4
329         ...     FRIDAY = 5
330         ...     SATURDAY = 6
331         ...     SUNDAY = 7
332         ...     @classmethod
333         ...     def today(cls):
334         ...         print('today is %s' % cls(date.today().isoweekday()).name)
335         ...
336         >>> dir(Weekday.SATURDAY)
337         ['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
338
339   .. method:: Enum._generate_next_value_(name, start, count, last_values)
340
341         :name: The name of the member being defined (e.g. 'RED').
342         :start: The start value for the Enum; the default is 1.
343         :count: The number of members currently defined, not including this one.
344         :last_values: A list of the previous values.
345
346      A *staticmethod* that is used to determine the next value returned by
347      :class:`auto`::
348
349         >>> from enum import auto
350         >>> class PowersOfThree(Enum):
351         ...     @staticmethod
352         ...     def _generate_next_value_(name, start, count, last_values):
353         ...         return 3 ** (count + 1)
354         ...     FIRST = auto()
355         ...     SECOND = auto()
356         ...
357         >>> PowersOfThree.SECOND.value
358         9
359
360   .. method:: Enum.__init__(self, *args, **kwds)
361
362      By default, does nothing.  If multiple values are given in the member
363      assignment, those values become separate arguments to ``__init__``; e.g.
364
365         >>> from enum import Enum
366         >>> class Weekday(Enum):
367         ...     MONDAY = 1, 'Mon'
368
369      ``Weekday.__init__()`` would be called as ``Weekday.__init__(self, 1, 'Mon')``
370
371   .. method:: Enum.__init_subclass__(cls, **kwds)
372
373      A *classmethod* that is used to further configure subsequent subclasses.
374      By default, does nothing.
375
376   .. method:: Enum._missing_(cls, value)
377
378      A *classmethod* for looking up values not found in *cls*.  By default it
379      does nothing, but can be overridden to implement custom search behavior::
380
381         >>> from enum import StrEnum
382         >>> class Build(StrEnum):
383         ...     DEBUG = auto()
384         ...     OPTIMIZED = auto()
385         ...     @classmethod
386         ...     def _missing_(cls, value):
387         ...         value = value.lower()
388         ...         for member in cls:
389         ...             if member.value == value:
390         ...                 return member
391         ...         return None
392         ...
393         >>> Build.DEBUG.value
394         'debug'
395         >>> Build('deBUG')
396         <Build.DEBUG: 'debug'>
397
398   .. method:: Enum.__new__(cls, *args, **kwds)
399
400      By default, doesn't exist.  If specified, either in the enum class
401      definition or in a mixin class (such as ``int``), all values given
402      in the member assignment will be passed; e.g.
403
404         >>> from enum import Enum
405         >>> class MyIntEnum(int, Enum):
406         ...     TWENTYSIX = '1a', 16
407
408      results in the call ``int('1a', 16)`` and a value of ``26`` for the member.
409
410      .. note::
411
412         When writing a custom ``__new__``, do not use ``super().__new__`` --
413         call the appropriate ``__new__`` instead.
414
415   .. method:: Enum.__repr__(self)
416
417      Returns the string used for *repr()* calls.  By default, returns the
418      *Enum* name, member name, and value, but can be overridden::
419
420         >>> class OtherStyle(Enum):
421         ...     ALTERNATE = auto()
422         ...     OTHER = auto()
423         ...     SOMETHING_ELSE = auto()
424         ...     def __repr__(self):
425         ...         cls_name = self.__class__.__name__
426         ...         return f'{cls_name}.{self.name}'
427         ...
428         >>> OtherStyle.ALTERNATE, str(OtherStyle.ALTERNATE), f"{OtherStyle.ALTERNATE}"
429         (OtherStyle.ALTERNATE, 'OtherStyle.ALTERNATE', 'OtherStyle.ALTERNATE')
430
431   .. method:: Enum.__str__(self)
432
433      Returns the string used for *str()* calls.  By default, returns the
434      *Enum* name and member name, but can be overridden::
435
436         >>> class OtherStyle(Enum):
437         ...     ALTERNATE = auto()
438         ...     OTHER = auto()
439         ...     SOMETHING_ELSE = auto()
440         ...     def __str__(self):
441         ...         return f'{self.name}'
442         ...
443         >>> OtherStyle.ALTERNATE, str(OtherStyle.ALTERNATE), f"{OtherStyle.ALTERNATE}"
444         (<OtherStyle.ALTERNATE: 1>, 'ALTERNATE', 'ALTERNATE')
445
446   .. method:: Enum.__format__(self)
447
448      Returns the string used for *format()* and *f-string* calls.  By default,
449      returns :meth:`__str__` return value, but can be overridden::
450
451         >>> class OtherStyle(Enum):
452         ...     ALTERNATE = auto()
453         ...     OTHER = auto()
454         ...     SOMETHING_ELSE = auto()
455         ...     def __format__(self, spec):
456         ...         return f'{self.name}'
457         ...
458         >>> OtherStyle.ALTERNATE, str(OtherStyle.ALTERNATE), f"{OtherStyle.ALTERNATE}"
459         (<OtherStyle.ALTERNATE: 1>, 'OtherStyle.ALTERNATE', 'ALTERNATE')
460
461   .. note::
462
463      Using :class:`auto` with :class:`Enum` results in integers of increasing value,
464      starting with ``1``.
465
466   .. versionchanged:: 3.12 Added :ref:`enum-dataclass-support`
467
468
469.. class:: IntEnum
470
471   *IntEnum* is the same as :class:`Enum`, but its members are also integers and can be
472   used anywhere that an integer can be used.  If any integer operation is performed
473   with an *IntEnum* member, the resulting value loses its enumeration status.
474
475      >>> from enum import IntEnum
476      >>> class Number(IntEnum):
477      ...     ONE = 1
478      ...     TWO = 2
479      ...     THREE = 3
480      ...
481      >>> Number.THREE
482      <Number.THREE: 3>
483      >>> Number.ONE + Number.TWO
484      3
485      >>> Number.THREE + 5
486      8
487      >>> Number.THREE == 3
488      True
489
490   .. note::
491
492      Using :class:`auto` with :class:`IntEnum` results in integers of increasing
493      value, starting with ``1``.
494
495   .. versionchanged:: 3.11 :meth:`~object.__str__` is now :meth:`!int.__str__` to
496      better support the *replacement of existing constants* use-case.
497      :meth:`~object.__format__` was already :meth:`!int.__format__` for that same reason.
498
499
500.. class:: StrEnum
501
502   ``StrEnum`` is the same as :class:`Enum`, but its members are also strings and can be used
503   in most of the same places that a string can be used.  The result of any string
504   operation performed on or with a *StrEnum* member is not part of the enumeration.
505
506   .. note::
507
508      There are places in the stdlib that check for an exact :class:`str`
509      instead of a :class:`str` subclass (i.e. ``type(unknown) == str``
510      instead of ``isinstance(unknown, str)``), and in those locations you
511      will need to use ``str(StrEnum.member)``.
512
513   .. note::
514
515      Using :class:`auto` with :class:`StrEnum` results in the lower-cased member
516      name as the value.
517
518   .. note::
519
520      :meth:`~object.__str__` is :meth:`!str.__str__` to better support the
521      *replacement of existing constants* use-case.  :meth:`~object.__format__` is likewise
522      :meth:`!str.__format__` for that same reason.
523
524   .. versionadded:: 3.11
525
526.. class:: Flag
527
528   ``Flag`` is the same as :class:`Enum`, but its members support the bitwise
529   operators ``&`` (*AND*), ``|`` (*OR*), ``^`` (*XOR*), and ``~`` (*INVERT*);
530   the results of those operations are (aliases of) members of the enumeration.
531
532   .. method:: __contains__(self, value)
533
534      Returns *True* if value is in self::
535
536         >>> from enum import Flag, auto
537         >>> class Color(Flag):
538         ...     RED = auto()
539         ...     GREEN = auto()
540         ...     BLUE = auto()
541         ...
542         >>> purple = Color.RED | Color.BLUE
543         >>> white = Color.RED | Color.GREEN | Color.BLUE
544         >>> Color.GREEN in purple
545         False
546         >>> Color.GREEN in white
547         True
548         >>> purple in white
549         True
550         >>> white in purple
551         False
552
553   .. method:: __iter__(self):
554
555      Returns all contained non-alias members::
556
557         >>> list(Color.RED)
558         [<Color.RED: 1>]
559         >>> list(purple)
560         [<Color.RED: 1>, <Color.BLUE: 4>]
561
562      .. versionadded:: 3.11
563
564   .. method:: __len__(self):
565
566      Returns number of members in flag::
567
568         >>> len(Color.GREEN)
569         1
570         >>> len(white)
571         3
572
573      .. versionadded:: 3.11
574
575   .. method:: __bool__(self):
576
577      Returns *True* if any members in flag, *False* otherwise::
578
579         >>> bool(Color.GREEN)
580         True
581         >>> bool(white)
582         True
583         >>> black = Color(0)
584         >>> bool(black)
585         False
586
587   .. method:: __or__(self, other)
588
589      Returns current flag binary or'ed with other::
590
591         >>> Color.RED | Color.GREEN
592         <Color.RED|GREEN: 3>
593
594   .. method:: __and__(self, other)
595
596      Returns current flag binary and'ed with other::
597
598         >>> purple & white
599         <Color.RED|BLUE: 5>
600         >>> purple & Color.GREEN
601         <Color: 0>
602
603   .. method:: __xor__(self, other)
604
605      Returns current flag binary xor'ed with other::
606
607         >>> purple ^ white
608         <Color.GREEN: 2>
609         >>> purple ^ Color.GREEN
610         <Color.RED|GREEN|BLUE: 7>
611
612   .. method:: __invert__(self):
613
614      Returns all the flags in *type(self)* that are not in *self*::
615
616         >>> ~white
617         <Color: 0>
618         >>> ~purple
619         <Color.GREEN: 2>
620         >>> ~Color.RED
621         <Color.GREEN|BLUE: 6>
622
623   .. method:: _numeric_repr_
624
625      Function used to format any remaining unnamed numeric values.  Default is
626      the value's repr; common choices are :func:`hex` and :func:`oct`.
627
628   .. note::
629
630      Using :class:`auto` with :class:`Flag` results in integers that are powers
631      of two, starting with ``1``.
632
633   .. versionchanged:: 3.11 The *repr()* of zero-valued flags has changed.  It
634      is now::
635
636         >>> Color(0) # doctest: +SKIP
637         <Color: 0>
638
639.. class:: IntFlag
640
641   ``IntFlag`` is the same as :class:`Flag`, but its members are also integers and can be
642   used anywhere that an integer can be used.
643
644      >>> from enum import IntFlag, auto
645      >>> class Color(IntFlag):
646      ...     RED = auto()
647      ...     GREEN = auto()
648      ...     BLUE = auto()
649      ...
650      >>> Color.RED & 2
651      <Color: 0>
652      >>> Color.RED | 2
653      <Color.RED|GREEN: 3>
654
655   If any integer operation is performed with an *IntFlag* member, the result is
656   not an *IntFlag*::
657
658        >>> Color.RED + 2
659        3
660
661   If a :class:`Flag` operation is performed with an *IntFlag* member and:
662
663   * the result is a valid *IntFlag*: an *IntFlag* is returned
664   * the result is not a valid *IntFlag*: the result depends on the :class:`FlagBoundary` setting
665
666   The :func:`repr` of unnamed zero-valued flags has changed.  It is now:
667
668      >>> Color(0)
669      <Color: 0>
670
671   .. note::
672
673      Using :class:`auto` with :class:`IntFlag` results in integers that are powers
674      of two, starting with ``1``.
675
676   .. versionchanged:: 3.11
677
678      :meth:`~object.__str__` is now :meth:`!int.__str__` to better support the
679      *replacement of existing constants* use-case.  :meth:`~object.__format__` was
680      already :meth:`!int.__format__` for that same reason.
681
682      Inversion of an :class:`!IntFlag` now returns a positive value that is the
683      union of all flags not in the given flag, rather than a negative value.
684      This matches the existing :class:`Flag` behavior.
685
686.. class:: ReprEnum
687
688   :class:`!ReprEnum` uses the :meth:`repr() <Enum.__repr__>` of :class:`Enum`,
689   but the :class:`str() <str>` of the mixed-in data type:
690
691   * :meth:`!int.__str__` for :class:`IntEnum` and :class:`IntFlag`
692   * :meth:`!str.__str__` for :class:`StrEnum`
693
694   Inherit from :class:`!ReprEnum` to keep the :class:`str() <str>` / :func:`format`
695   of the mixed-in data type instead of using the
696   :class:`Enum`-default :meth:`str() <Enum.__str__>`.
697
698
699   .. versionadded:: 3.11
700
701.. class:: EnumCheck
702
703   *EnumCheck* contains the options used by the :func:`verify` decorator to ensure
704   various constraints; failed constraints result in a :exc:`ValueError`.
705
706   .. attribute:: UNIQUE
707
708      Ensure that each value has only one name::
709
710         >>> from enum import Enum, verify, UNIQUE
711         >>> @verify(UNIQUE)
712         ... class Color(Enum):
713         ...     RED = 1
714         ...     GREEN = 2
715         ...     BLUE = 3
716         ...     CRIMSON = 1
717         Traceback (most recent call last):
718         ...
719         ValueError: aliases found in <enum 'Color'>: CRIMSON -> RED
720
721
722   .. attribute:: CONTINUOUS
723
724      Ensure that there are no missing values between the lowest-valued member
725      and the highest-valued member::
726
727         >>> from enum import Enum, verify, CONTINUOUS
728         >>> @verify(CONTINUOUS)
729         ... class Color(Enum):
730         ...     RED = 1
731         ...     GREEN = 2
732         ...     BLUE = 5
733         Traceback (most recent call last):
734         ...
735         ValueError: invalid enum 'Color': missing values 3, 4
736
737   .. attribute:: NAMED_FLAGS
738
739      Ensure that any flag groups/masks contain only named flags -- useful when
740      values are specified instead of being generated by :func:`auto`::
741
742         >>> from enum import Flag, verify, NAMED_FLAGS
743         >>> @verify(NAMED_FLAGS)
744         ... class Color(Flag):
745         ...     RED = 1
746         ...     GREEN = 2
747         ...     BLUE = 4
748         ...     WHITE = 15
749         ...     NEON = 31
750         Traceback (most recent call last):
751         ...
752         ValueError: invalid Flag 'Color': aliases WHITE and NEON are missing combined values of 0x18 [use enum.show_flag_values(value) for details]
753
754   .. note::
755
756      CONTINUOUS and NAMED_FLAGS are designed to work with integer-valued members.
757
758   .. versionadded:: 3.11
759
760.. class:: FlagBoundary
761
762   ``FlagBoundary`` controls how out-of-range values are handled in :class:`Flag` and its
763   subclasses.
764
765   .. attribute:: STRICT
766
767      Out-of-range values cause a :exc:`ValueError` to be raised. This is the
768      default for :class:`Flag`::
769
770         >>> from enum import Flag, STRICT, auto
771         >>> class StrictFlag(Flag, boundary=STRICT):
772         ...     RED = auto()
773         ...     GREEN = auto()
774         ...     BLUE = auto()
775         ...
776         >>> StrictFlag(2**2 + 2**4)
777         Traceback (most recent call last):
778         ...
779         ValueError: <flag 'StrictFlag'> invalid value 20
780             given 0b0 10100
781           allowed 0b0 00111
782
783   .. attribute:: CONFORM
784
785      Out-of-range values have invalid values removed, leaving a valid :class:`Flag`
786      value::
787
788         >>> from enum import Flag, CONFORM, auto
789         >>> class ConformFlag(Flag, boundary=CONFORM):
790         ...     RED = auto()
791         ...     GREEN = auto()
792         ...     BLUE = auto()
793         ...
794         >>> ConformFlag(2**2 + 2**4)
795         <ConformFlag.BLUE: 4>
796
797   .. attribute:: EJECT
798
799      Out-of-range values lose their :class:`Flag` membership and revert to :class:`int`.
800
801         >>> from enum import Flag, EJECT, auto
802         >>> class EjectFlag(Flag, boundary=EJECT):
803         ...     RED = auto()
804         ...     GREEN = auto()
805         ...     BLUE = auto()
806         ...
807         >>> EjectFlag(2**2 + 2**4)
808         20
809
810   .. attribute:: KEEP
811
812      Out-of-range values are kept, and the :class:`Flag` membership is kept.
813      This is the default for :class:`IntFlag`::
814
815         >>> from enum import Flag, KEEP, auto
816         >>> class KeepFlag(Flag, boundary=KEEP):
817         ...     RED = auto()
818         ...     GREEN = auto()
819         ...     BLUE = auto()
820         ...
821         >>> KeepFlag(2**2 + 2**4)
822         <KeepFlag.BLUE|16: 20>
823
824.. versionadded:: 3.11
825
826---------------
827
828Supported ``__dunder__`` names
829""""""""""""""""""""""""""""""
830
831:attr:`~EnumType.__members__` is a read-only ordered mapping of ``member_name``:``member``
832items.  It is only available on the class.
833
834:meth:`~Enum.__new__`, if specified, must create and return the enum members;
835it is also a very good idea to set the member's :attr:`!_value_` appropriately.
836Once all the members are created it is no longer used.
837
838
839Supported ``_sunder_`` names
840""""""""""""""""""""""""""""
841
842- :meth:`~EnumType._add_alias_` -- adds a new name as an alias to an existing
843  member.
844- :meth:`~EnumType._add_value_alias_` -- adds a new value as an alias to an
845  existing member.
846- :attr:`~Enum._name_` -- name of the member
847- :attr:`~Enum._value_` -- value of the member; can be set in ``__new__``
848- :meth:`~Enum._missing_` -- a lookup function used when a value is not found;
849  may be overridden
850- :attr:`~Enum._ignore_` -- a list of names, either as a :class:`list` or a
851  :class:`str`, that will not be transformed into members, and will be removed
852  from the final class
853- :attr:`~Enum._order_` -- no longer used, kept for backward
854  compatibility (class attribute, removed during class creation)
855- :meth:`~Enum._generate_next_value_` -- used to get an appropriate value for
856  an enum member; may be overridden
857
858  .. note::
859
860     For standard :class:`Enum` classes the next value chosen is the highest
861     value seen incremented by one.
862
863     For :class:`Flag` classes the next value chosen will be the next highest
864     power-of-two.
865
866- While ``_sunder_`` names are generally reserved for the further development
867  of the :class:`Enum` class and can not be used, some are explicitly allowed:
868
869  - ``_repr_*`` (e.g. ``_repr_html_``), as used in `IPython's rich display`_
870
871.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_``
872.. versionadded:: 3.7 ``_ignore_``
873.. versionadded:: 3.13 ``_add_alias_``, ``_add_value_alias_``, ``_repr_*``
874.. _`IPython's rich display`: https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
875
876---------------
877
878Utilities and Decorators
879------------------------
880
881.. class:: auto
882
883   *auto* can be used in place of a value.  If used, the *Enum* machinery will
884   call an :class:`Enum`'s :meth:`~Enum._generate_next_value_` to get an appropriate value.
885   For :class:`Enum` and :class:`IntEnum` that appropriate value will be the last value plus
886   one; for :class:`Flag` and :class:`IntFlag` it will be the first power-of-two greater
887   than the highest value; for :class:`StrEnum` it will be the lower-cased version of
888   the member's name.  Care must be taken if mixing *auto()* with manually
889   specified values.
890
891   *auto* instances are only resolved when at the top level of an assignment:
892
893   * ``FIRST = auto()`` will work (auto() is replaced with ``1``);
894   * ``SECOND = auto(), -2`` will work (auto is replaced with ``2``, so ``2, -2`` is
895     used to create the ``SECOND`` enum member;
896   * ``THREE = [auto(), -3]`` will *not* work (``<auto instance>, -3`` is used to
897     create the ``THREE`` enum member)
898
899   .. versionchanged:: 3.11.1
900
901      In prior versions, ``auto()`` had to be the only thing
902      on the assignment line to work properly.
903
904   ``_generate_next_value_`` can be overridden to customize the values used by
905   *auto*.
906
907   .. note:: in 3.13 the default ``_generate_next_value_`` will always return
908             the highest member value incremented by 1, and will fail if any
909             member is an incompatible type.
910
911.. decorator:: property
912
913   A decorator similar to the built-in *property*, but specifically for
914   enumerations.  It allows member attributes to have the same names as members
915   themselves.
916
917   .. note:: the *property* and the member must be defined in separate classes;
918             for example, the *value* and *name* attributes are defined in the
919             *Enum* class, and *Enum* subclasses can define members with the
920             names ``value`` and ``name``.
921
922   .. versionadded:: 3.11
923
924.. decorator:: unique
925
926   A :keyword:`class` decorator specifically for enumerations.  It searches an
927   enumeration's :attr:`~EnumType.__members__`, gathering any aliases it finds; if any are
928   found :exc:`ValueError` is raised with the details::
929
930      >>> from enum import Enum, unique
931      >>> @unique
932      ... class Mistake(Enum):
933      ...     ONE = 1
934      ...     TWO = 2
935      ...     THREE = 3
936      ...     FOUR = 3
937      ...
938      Traceback (most recent call last):
939      ...
940      ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
941
942.. decorator:: verify
943
944   A :keyword:`class` decorator specifically for enumerations.  Members from
945   :class:`EnumCheck` are used to specify which constraints should be checked
946   on the decorated enumeration.
947
948   .. versionadded:: 3.11
949
950.. decorator:: member
951
952   A decorator for use in enums: its target will become a member.
953
954   .. versionadded:: 3.11
955
956.. decorator:: nonmember
957
958   A decorator for use in enums: its target will not become a member.
959
960   .. versionadded:: 3.11
961
962.. decorator:: global_enum
963
964   A decorator to change the :class:`str() <str>` and :func:`repr` of an enum
965   to show its members as belonging to the module instead of its class.
966   Should only be used when the enum members are exported
967   to the module global namespace (see :class:`re.RegexFlag` for an example).
968
969
970   .. versionadded:: 3.11
971
972.. function:: show_flag_values(value)
973
974   Return a list of all power-of-two integers contained in a flag *value*.
975
976   .. versionadded:: 3.11
977
978---------------
979
980Notes
981-----
982
983:class:`IntEnum`, :class:`StrEnum`, and :class:`IntFlag`
984
985   These three enum types are designed to be drop-in replacements for existing
986   integer- and string-based values; as such, they have extra limitations:
987
988   - ``__str__`` uses the value and not the name of the enum member
989
990   - ``__format__``, because it uses ``__str__``, will also use the value of
991     the enum member instead of its name
992
993   If you do not need/want those limitations, you can either create your own
994   base class by mixing in the ``int`` or ``str`` type yourself::
995
996       >>> from enum import Enum
997       >>> class MyIntEnum(int, Enum):
998       ...     pass
999
1000   or you can reassign the appropriate :meth:`str`, etc., in your enum::
1001
1002       >>> from enum import Enum, IntEnum
1003       >>> class MyIntEnum(IntEnum):
1004       ...     __str__ = Enum.__str__
1005