Mailing List Archive

[issue38250] enum.Flag should be more set-like
Change by Ethan Furman <ethan@stoneleaf.us>:


----------
pull_requests: +23042
pull_request: https://github.com/python/cpython/pull/24215

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
Ethan Furman <ethan@stoneleaf.us> added the comment:

The code sample:

class Color(IntFlag):
BLACK = 0
RED = 1
GREEN = 2
BLUE = 4
PURPLE = RED | BLUE
WHITE = RED | GREEN | BLUE

Here's the summary of the changes:

- single-bit flags are canonical
- multi-bit and zero-bit flags are aliases
+ only canonical flags are returned during iteration

>>> list(Color.WHITE)
[<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]

- negating a flag or flag set returns a new flag/flag set with the
corresponding positive integer value

>> Color.GREEN
<Color.GREEN: 2>

>> ~Color.GREEN
<Color.PURPLE: 5>

- `name`s of pseudo-flags are constructed from their members' names

>>> (Color.RED | Color.GREEN).name
'RED|GREEN'

- multi-bit flags, aka aliases, can be returned from operations

>>> Color.RED | Color.BLUE
<Color.PURPLE: 5>

>>> Color(7) # or Color(-1)
<Color.WHITE: 7>

- membership / containment checking has changed slightly -- zero valued flags
are never considered to be contained:

>>> Color.BLACK in Color.WHITE
False

otherwise, if all bits of one flag are in the other flag, True is returned:

>>> Color.PURPLE in Color.WHITE
True

There is a new boundary mechanism that controls how out-of-range / invalid bits are handled: `STRICT`, `CONFORM`, `EJECT', and `KEEP':

STRICT --> raises an exception when presented with invalid values
CONFORM --> discards any invalid bits
EJECT --> lose Flag status and become a normal int with the given value
KEEP --> keep the extra bits
- keeps Flag status and extra bits
- they don't show up in iteration
- they do show up in repr() and str()

The default for Flag is STRICT, the default for IntFlag is DISCARD, and the default for _convert_ is KEEP (see ssl.Options for an example of when KEEP is needed).

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
Ethan Furman <ethan@stoneleaf.us> added the comment:


New changeset 7aaeb2a3d682ecba125c33511e4b4796021d2f82 by Ethan Furman in branch 'master':
bpo-38250: [Enum] single-bit flags are canonical (GH-24215)
https://github.com/python/cpython/commit/7aaeb2a3d682ecba125c33511e4b4796021d2f82


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
Ethan Furman <ethan@stoneleaf.us> added the comment:

Thank you to everyone involved. :-)

To answer the first three points that started this issue:

1. iteration -> each single-bit flag in the entire flag, or a
combinations of flags, is returned one at a time -- not the
empty set, not other multi-bit values

2. length is implemented -> `len(Color.BLUE | Color.RED) == 2`

3. subset is implemented as containment checking:
`Color.BLUE in (Color.RED | Color.BLUE) is True`

----------
resolution: -> fixed
stage: patch review -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

test_enum fails when Python is installed:

PPC64LE Fedora Rawhide Clang Installed 3.x:
https://buildbot.python.org/all/#builders/312/builds/597

0:01:37 load avg: 8.99 [232/426/1] test_enum failed -- running: test_tokenize (1 min 37 sec), test_unparse (31.7 sec), test_concurrent_futures (37.5 sec)
Failed to call load_tests:
Traceback (most recent call last):
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/unittest/loader.py", line 130, in loadTestsFromModule
return load_tests(self, tests, pattern)
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/test/test_enum.py", line 20, in load_tests
tests.addTests(doctest.DocFileSuite(
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/doctest.py", line 2511, in DocFileSuite
suite.addTest(DocFileTest(path, **kw))
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/doctest.py", line 2433, in DocFileTest
doc, path = _load_testfile(path, package, module_relative,
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/doctest.py", line 231, in _load_testfile
file_contents = loader.get_data(filename)
File "<frozen importlib._bootstrap_external>", line 1023, in get_data
FileNotFoundError: [Errno 2] No such file or directory: '/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/test/../../Doc/library/enum.rst'

test test_enum crashed -- Traceback (most recent call last):
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/test/libregrtest/runtest.py", line 272, in _runtest_inner
refleak = _runtest_inner2(ns, test_name)
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/test/libregrtest/runtest.py", line 236, in _runtest_inner2
test_runner()
File "/home/buildbot/buildarea/3.x.cstratak-fedora-rawhide-ppc64le.clang-installed/build/target/lib/python3.10/test/libregrtest/runtest.py", line 210, in _test_module
raise Exception("errors while loading tests")
Exception: errors while loading tests


Tests are loaded by Lib/test/test_enum.py with:

def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(enum))
tests.addTests(doctest.DocFileSuite(
'../../Doc/library/enum.rst',
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
))
return tests

----------
nosy: +vstinner
resolution: fixed ->
status: closed -> open

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
Change by Ethan Furman <ethan@stoneleaf.us>:


----------
pull_requests: +23161
stage: resolved -> patch review
pull_request: https://github.com/python/cpython/pull/24342

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
Ethan Furman <ethan@stoneleaf.us> added the comment:


New changeset 01faf4542a8652adfbd3b3f897ba718e8ce43f5e by Ethan Furman in branch 'master':
bpo-38250: [Enum] only include .rst test if file available (GH-24342)
https://github.com/python/cpython/commit/01faf4542a8652adfbd3b3f897ba718e8ce43f5e


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue38250] enum.Flag should be more set-like [ In reply to ]
STINNER Victor <vstinner@python.org> added the comment:

PPC64LE Fedora Rawhide Clang Installed 3.x is back to green, thanks for the fix ;-)
https://buildbot.python.org/all/#/builders/312/builds/600

----------
resolution: -> fixed
stage: patch review -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue38250>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com