PyQt6 Enum implcit conversion

Julien Cabieces julien.cabieces at oslandia.com
Thu May 25 15:25:41 BST 2023


The same example is working in pure python

---
class MyEnum(IntEnum):
   VALA=0
   VALB=1
   VALC=2

def printEnum(an_enum):
   if an_enum == MyEnum.VALA:
      print("VALA")
   else:
      print("others")

printEnum(1)
others
---

It makes sense to me for an IntEnum to be able to convert from/to an
integer, no?

Julien


> They are still different types so you need to explicitly convert the
> int.
>
> Phil
>
> On 25/05/2023 14:15, Julien Cabieces wrote:
>> Yes, I tried that also with /BaseType=IntEnum/, and I also get the same
>> issue even if the pyi generated file seems to be correctly generated
>> class MyEnum(enum.IntEnum):
>>         VALA = ... # type: Test.MyEnum
>>         VALB = ... # type: Test.MyEnum
>>         VALC = ... # type: Test.MyEnum
>> Regards,
>> Julien
>> 
>>> On 25/05/2023 12:37, Julien Cabieces wrote:
>>>> Hi,
>>>> With SIP/PyQT5 it was possible to pass directly an int to a function
>>>> taking an enum as a parameter. It's no longer possible with PyQt6.
>>>> For instance, with the following attached sip/header files, in PyQt5
>>>> the
>>>> following code works :
>>>> $ python -c "from test import Test;test=Test();test.printEnum(2)"
>>>> others%
>>>> While in PyQt6 it display the following error:
>>>> Traceback (most recent call last):
>>>>   File "<string>", line 1, in <module>
>>>> TypeError: printEnum(self, e: Test.MyEnum): argument 1 has
>>>> unexpected type 'int'
>>>> I understand that the issue is due to the fact that from now enum
>>>> inherit from Python enum.Enum type instead of int, but is this
>>>> behavior
>>>> wanted or is it an issue that would be fixed ?
>>> It's wanted. If you want int behaviour then annotate the enum
>>> definition...
>>> https://www.riverbankcomputing.com/static/Docs/sip/annotations.html#enum-annotation-BaseType
>>> Phil


-- 

Julien Cabieces
Senior Developer at Oslandia
julien.cabieces at oslandia.com
09.72.52.52.76


More information about the PyQt mailing list