Make comtypes generated files IDE-friendly (#18564)
Fixes #17608
Summary of the issue:
Since newer versions of comtypes changed the generated comtypes.gen module code, the function makeIDEFriendly in comInterfaces_sconscript no longer patches the generated files.
Some background: NVDA uses its own custom folder, comInterfaces, for the modules generated by comtypes, which would by default be put under comtypes.gen. Although special care is taken to make sure comtypes.gen.SomeLibrary can still reference the module in comInterfaces, IDEs such as VS Code is not smart enough to figure that out, and continue to find those modules in comtypes.gen, where those modules don't exist because they have been moved to comInterfaces.
makeIDEFriendly was created to address this issue by replacing the following lines in the generated code:
from comtypes.gen import SomeLib
with something like this:
try:
from comtypes.gen import SomeLib # works fine at runtime
except ModuleNotFoundError:
import SomeLib # fallback for static analyzers, import directly from `comInterfaces`
from SomeLib import *
but it stops working now, because comtypes now generates the following code:
import comtypes.gen._SomeLibId_ as __wrapper_module__
from comtypes.gen._SomeLibId_ import (
# a long list of all individual imported items
)
Description of user facing changes:
None
Description of developer facing changes:
IDEs such as VS Code will be able to locate COM module imports again.
Description of development approach:
Change makeIDEFriendly, so now it replaces the following:
from comtypes.gen._SomeLibId_ import (
# a long list of all individual imported items
)
with the following:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ._SomeLibId_ import (
# a long list of all individual imported items
)
else:
from comtypes.gen._SomeLibId_ import (
# a long list of all individual imported items
)