Repository navigation
How to get Pylance (VS Code) to play nice with DPG? #2670
Replies: 1 comment
|
Hey @blargh-sys, I reproduced this locally and it's not really Pylance's fault. The annotations in the generated @contextmanager
def window(*, label: str = None, ..., **kwargs) -> Union[int, str]:
...
yield widget
Workaround without ignores: a local stub. Pylance reads npx pyright --createstub dearpygui.dearpygui
python fix_dpg_stub.py typings/dearpygui/dearpygui.pyi# fix_dpg_stub.py
import sys
path = sys.argv[1]
out, pending = [], False
for line in open(path):
if line.startswith("@contextmanager"):
pending = True
elif pending and "-> Union[int, str]:" in line:
line = line.replace("-> Union[int, str]:", "-> Iterator[Union[int, str]]:", 1)
pending = False
elif pending and line.rstrip().endswith(":") and "->" in line:
pending = False
out.append(line)
src = "".join(out).replace("from typing import ", "from typing import Iterator, ", 1)
open(path, "w").write(src)It fixes 41 context managers. After that Real fix would be in stream << ", **kwargs) -> ";
stream << PythonDataTypeActual(parser.second.returnType) << ":";Wrapping it in Hope that helps, it's a bit hacky but it saves you from ignores everywhere. |
Uh oh!
There was an error while loading. Please reload this page.
Consider the demo app from the project's README. It uses Python's context manager protocol for the
windowfunction using thewithkeyword. In DPG's source, thewindowfunction is wrapped with a@contextmanagerdecorator, but in Pylance's opinion the call returns simple union of int and str, not a context manager that wraps it, thus failing its type check.How to deal with this without type ignores or casting each context managed function?
All reactions