-
Suggestion
-
Resolution: Fixed
-
Not Evaluated
-
6.8.2
-
None
-
45b9a7de8 (dev), 0a0b59d82 (6.9), 822397986 (tqtc/lts-6.8)
All Qt enums have been declared as classes inheriting from enum.Enum . This covers many of the use cases of using Qt enums in python, but fails one (from typing point of view) : getting the enum value.
For example, I have some code like this :
def some_func() -> int;
ret: int
p = QProcess()
[run qprocess]
if p.exitStatus() != QProcess.ExitStatus.NormalExit:
ret = p.exitStatus().value
else:
ret = p.exitCode()
return ret
Getting the value of p.exitStatus() will not type correctly because it enum.Enum values have not type specified.
Proposition: instead of using Enum as your base class for Qt enum, use IntEnum . This will reflect the practical reality, all Qt enum are actually int enum. And this will type correctly.
I tested it successfully in PySide6-stubs:
class ExitStatus(enum.IntEnum):
NormalExit = 0x0
CrashExit = 0x1
Note that contrary to my title, using IntEnum is not sufficient. You have to actually provide a value or declare explicitely the enum as int :
class ExitStatus(enum.IntEnum):
NormalExit : int = ... # 0x0
CrashExit : int = ... # 0x1