8000 bpo-38250: [Enum] single-bit flags are canonical by ethanfurman · Pull Request #24215 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-38250: [Enum] single-bit flags are canonical #24215

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 30 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5f522d3
[Enum] fix Flag iteration, repr(), and str()
ethanfurman Jan 13, 2021
f7f9e72
add boundary KEEP for Flags (default for _convert_)
ethanfurman Jan 14, 2021
d289ba3
update re.RegexFlag for new Flag implementation
ethanfurman Jan 14, 2021
72dbdd7
remove extra white space
ethanfurman Jan 14, 2021
210fae7
test that zero-valued members are empty
ethanfurman Jan 14, 2021
48bcd07
update tests to confirm CONFORM with negate
ethanfurman Jan 14, 2021
1fd7471
update aenum.rst; add doctest to test_enum
ethanfurman Jan 14, 2021
aa425e6
fix doc test
ethanfurman Jan 14, 2021
4786942
optimizations
ethanfurman Jan 14, 2021
45565b2
formatting
ethanfurman Jan 14, 2021
806c8c6
add news entry
ethanfurman Jan 14, 2021
668c9a9
fix formatting of news entry
ethanfurman Jan 15, 2021
9bd9e97
update iteration method and order
ethanfurman Jan 20, 2021
18bcbac
add John Belmonte
ethanfurman Jan 20, 2021
f1c4584
more bit-fiddling improvements
ethanfurman Jan 20, 2021
e3713aa
use pop() instead of "del"
ethanfurman Jan 20, 2021
c4ec211
update DynamicClassAttribute __doc__
ethanfurm 8000 an Jan 20, 2021
00b2bfe
remove formatting changes
ethanfurman Jan 20, 2021
9f432c3
remove extra parens
ethanfurman Jan 20, 2021
15c060a
remove formatting changes
ethanfurman Jan 20, 2021
86d7669
remove formatting
ethanfurman Jan 20, 2021
55915df
simplify determination of member iteration
ethanfurman Jan 22, 2021
95bf9c8
add note about next auto() value for Enum and Flag
ethanfurman Jan 25, 2021
41ac1ce
local name optimizations
ethanfurman Jan 25, 2021
3ea814e
remove commented-out code
ethanfurman Jan 25, 2021
4983558
add test for next auto() and _order_
ethanfurman Jan 25, 2021
651da18
raise TypeError if _value_ not added in custom new
ethanfurman Jan 25, 2021
6e99d48
enable doc tests, update formatting
ethanfurman Jan 25, 2021
b52c5a2
fix note
ethanfurman Jan 25, 2021
8d7b272
Update 2021-01-14-15-07-16.bpo-38250.1fvhOk.rst
ethanfurman Jan 25, 2021
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
Next Next commit
[Enum] fix Flag iteration, repr(), and str()
Flag members are now divided by one-bit verses multi-bit, with multi-bit
being treated as aliases.  Iterating over a flag only returns the
contained single-bit flags.

repr() and str() now only show the Flags, not extra integer values; any
extra integer values are either discarded (CONFORM), turned into
``int``s (EJECT) or treated as errors (STRICT).  Flag classes can
specify which of those three behaviors is desired:

    >>> class Test(Flag, boundary=CONFORM):
    ...     ONE = 1
    ...     TWO = 2
    ...
    >>> Test(5)
    <Test.ONE: 1>
  • Loading branch information
ethanfurman committed Jan 14, 2021
commit 5f522d38139dc6d9bd7781500f811016b683b66f
46 changes: 38 additions & 8 deletions Doc/library/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,22 @@ IntFlag
The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based
on :class:`int`. The difference being :class:`IntFlag` members can be combined
using the bitwise operators (&, \|, ^, ~) and the result is still an
:class:`IntFlag` member. However, as the name implies, :class:`IntFlag`
:class:`IntFlag` member, if possible. However, as the name implies, :class:`IntFlag`
members also subclass :class:`int` and can be used wherever an :class:`int` is
used. Any operation on an :class:`IntFlag` member besides the bit-wise
operations will lose the :class:`IntFlag` membership.
used.

.. note::

Any operation on an :class:`IntFlag` member besides the bit-wise operations will
lose the :class:`IntFlag` membership.

.. note::

Bit-wise operations that result in invalid :class:`IntFlag` values will lose the
:class:`IntFlag` membership.

.. versionadded:: 3.6
.. versionchanged:: 3.10

Sample :class:`IntFlag` class::

Expand Down Expand Up @@ -671,21 +681,41 @@ It is also possible to name the combinations::
>>> Perm.RWX
<Perm.RWX: 7>
>>> ~Perm.RWX
<Perm.-8: -8>
<Perm: 0>
>>> Perm(7)
<Perm.RWX: 7>

.. note::

Named combinations are considered aliases. Aliases do not show up during
iteration, but can be returned from by-value lookups.

.. versionchanged:: 3.10

Another important difference between :class:`IntFlag` and :class:`Enum` is that
if no flags are set (the value is 0), its boolean evaluation is :data:`False`::

>>> Perm.R & Perm.X
<Perm.0: 0>
<Perm: 0>
>>> bool(Perm.R & Perm.X)
False

Because :class:`IntFlag` members are also subclasses of :class:`int` they can
be combined with them::
be combined with them (but may lose :class:`IntFlag` membership::

>>> Perm.X | 4
<Perm.R|X: 5>

>>> Perm.X | 8
<Perm.8|X: 9>
9

.. note::

The negation operator, ``~``, always returns an :class:`IntFlag` member with a
positive number::

>>> ~Perm.X
<Perm.R|W: 6>

:class:`IntFlag` members can also be iterated over::

Expand Down Expand Up @@ -717,7 +747,7 @@ flags being set, the boolean evaluation is :data:`False`::
... GREEN = auto()
...
>>> Color.RED & Color.GREEN
<Color.0: 0>
<Color: 0>
>>> bool(Color.RED & Color.GREEN)
False

Expand Down
Loading
0