]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-107538: [Enum] fix handling of inverted/negative values (GH-132273)
authorEthan Furman <ethan@stoneleaf.us>
Thu, 10 Jul 2025 23:49:09 +0000 (16:49 -0700)
committerGitHub <noreply@github.com>
Thu, 10 Jul 2025 23:49:09 +0000 (16:49 -0700)
commit49365bd110a254a6a304d2f880482bfeb2e4490d
tree530b7560ee09b05b3878265236c15cda081a2a33
parent56c6f04b8862c20ff6eddc4400f170ad91e55f66
gh-107538: [Enum] fix handling of inverted/negative values (GH-132273)

* Fix flag mask inversion when unnamed flags exist.

For example:

    class Flag(enum.Flag):
        A = 0x01
        B = 0x02
        MASK = 0xff

    ~Flag.MASK is Flag(0)

* EJECT and KEEP flags (IntEnum is KEEP) use direct value.

* correct Flag inversion to only flip flag bits

IntFlag will flip all bits -- this only makes a difference in flag sets with
missing values.

* correct negative assigned values in flags

negative values are no longer used as-is, but become inverted; i.e.

    class Y(self.enum_type):
        A = auto()
        B = auto()
        C = ~A        # aka ~1 aka 0b1 110 (from enum.bin()) aka 6
        D = auto()

    assert Y.C. is Y.B|Y.D
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2023-07-05-14-34-10.gh-issue-105497.HU5u89.rst [new file with mode: 0644]
Misc/NEWS.d/next/Library/2025-04-08-07-25-10.gh-issue-107583.JGfbhq.rst [new file with mode: 0644]