Skip to content

Make comtypes generated files IDE-friendly#18564

Merged
seanbudd merged 3 commits into
nvaccess:masterfrom
gexgd0419:make-com-ide-friendly
Jul 29, 2025
Merged

Make comtypes generated files IDE-friendly#18564
seanbudd merged 3 commits into
nvaccess:masterfrom
gexgd0419:make-com-ide-friendly

Conversation

@gexgd0419

@gexgd0419 gexgd0419 commented Jul 27, 2025

Copy link
Copy Markdown
Contributor

Link to issue number:

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
    )

Testing strategy:

Regenerated the COM modules using ./scons source.

Known issues with pull request:

The fix might break again with a future comtypes version.

Code Review Checklist:

  • Documentation:
    • Change log entry
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • API is compatible with existing add-ons.
  • Security precautions taken.

@coderabbitai summary

@gexgd0419 gexgd0419 marked this pull request as ready for review July 27, 2025 07:03
@gexgd0419 gexgd0419 requested a review from a team as a code owner July 27, 2025 07:04
@gexgd0419 gexgd0419 requested a review from seanbudd July 27, 2025 07:04
@hwf1324

hwf1324 commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

This PR lacks an entry in the changelog.

@seanbudd seanbudd added the conceptApproved Similar 'triaged' for issues, PR accepted in theory, implementation needs review. label Jul 29, 2025
Comment thread source/comInterfaces_sconscript Outdated
@seanbudd

Copy link
Copy Markdown
Member

Thanks @gexgd0419

@seanbudd seanbudd enabled auto-merge (squash) July 29, 2025 01:55
@seanbudd seanbudd merged commit 3f8b197 into nvaccess:master Jul 29, 2025
22 checks passed
@gexgd0419 gexgd0419 deleted the make-com-ide-friendly branch August 12, 2025 02:49
@SaschaCowley SaschaCowley added this to the 2025.3 milestone Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conceptApproved Similar 'triaged' for issues, PR accepted in theory, implementation needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Developer: IDE-friendly function matching for comInterfaces fails and the IDE cannot find the definition.

4 participants