pthom / imgui_bundle

Your fast track to powerful GUIs: Dear ImGui Bundle, an extensive toolkit for Python and C++ with immediate mode efficiency.
https://pthom.github.io/imgui_bundle/
MIT License
703 stars 73 forks source link

KeyChord type not found at runtime for type annotations #257

Closed oxysoft closed 1 month ago

oxysoft commented 1 month ago

Code:

from imgui_bundle import imgui

def test() -> imgui.KeyChord:
    pass

if __name__ == '__main__':
    print("Hello, world!")

Stacktrace

Traceback (most recent call last):
  File "/home/nuck/.local/share/JetBrains/Toolbox/apps/pycharm-community/plugins/python-ce/helpers/pydev/pydevd.py", line 1534, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nuck/.local/share/JetBrains/Toolbox/apps/pycharm-community/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/nuck/PycharmProjects/imgui-stub-test/main.py", line 3, in <module>
    def test() -> imgui.KeyChord:
                  ^^^^^^^^^^^^^^
AttributeError: module 'imgui_bundle._imgui_bundle.imgui' has no attribute 'KeyChord'
pthom commented 1 month ago

See https://github.com/pthom/imgui_bundle/blob/564cf51ebeba07e9d3173c62499522c36afbdae3/bindings/imgui_bundle/imgui/__init__.pyi#L177-L192

ModFlags = int  # -> enum ModFlags_        # Flags: for io.KeyMods (Ctrl/Shift/Alt/Super)
...
KeyChord = ModFlags  # == int. We generally use ImGuiKeyChord to mean "a ImGuiKey or-ed with any number of

KeyCoord is likely only defined in the stub, which is not a real definition. Use ModFlags_ or int instead.

This is an non standard API in imgui.h anyhow: ImGuiKeyChord is an int which can be selected inside the enum ImGuiKey or the enum ImGuiMod_

typedef int ImGuiKeyChord;          // -> ImGuiKey | ImGuiMod_XXX    // Flags: for IsKeyChordPressed(), Shortcut() etc. an ImGuiKey optionally OR-ed with one or more ImGuiMod_XXX values.