Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-3048

typing: use IntEnum instead of enum

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Incomplete
    • Not Evaluated
    • None
    • 6.8.2
    • Type hints
    • None

    Description

      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

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ctismer Christian Tismer
            bluebird75 Philippe Fremy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes