Python 3.11.4 breaks inverting flags

Florian Bruhin me at the-compiler.org
Thu Jun 8 13:44:03 BST 2023


Hey,

It looks like Python 3.11.4 breaks inverting of enum.Flag when there is
a "mask" member set (like e.g. with Qt.KeyboardModifier).

With Python 3.11.3:

    >>> combined = Qt.KeyboardModifier.ShiftModifier | Qt.KeyboardModifier.ControlModifier
    >>> combined & ~Qt.KeyboardModifier.ControlModifier
    <KeyboardModifier.ShiftModifier: 33554432>

But with 3.11.4:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.11/enum.py", line 1542, in __invert__
        self._inverted_ = self.__class__(self._flag_mask_ ^ self._value_)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/enum.py", line 711, in __call__
        return cls.__new__(cls, value)
               ^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/enum.py", line 1136, in __new__
        raise exc
      File "/usr/lib/python3.11/enum.py", line 1113, in __new__
        result = cls._missing_(value)
                 ^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/enum.py", line 1454, in _missing_
        raise ValueError('%r: no members with value %r' % (cls, unknown))
    ValueError: <flag 'KeyboardModifier'>: no members with value 2147483648

As a (rather annoying) workaround, a detour via int works:

    >>> Qt.KeyboardModifier(combined.value & ~Qt.KeyboardModifier.ControlModifier.value)
    <KeyboardModifier.ShiftModifier: 33554432>

It seems to be a Python issue, as it's trivially reproducable with:

    import enum

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

    print(~Flag.A)

I opened an issue against CPython:
https://github.com/python/cpython/issues/105497

Florian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230608/1718b514/attachment.sig>


More information about the PyQt mailing list