Skip to content

Reorganize automation - typeinfo module dependency structure. - #987

Merged
junkmd merged 3 commits into
safearray_typeinfo_depfrom
typeinfo_automation_dep_dir
Nov 12, 2026
Merged

junkmd merged 3 commits into
safearray_typeinfo_depfrom
typeinfo_automation_dep_dir

Conversation

@junkmd

@junkmd junkmd commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

This package models COM, but a Python module also models its complexity.
Because the modules depend on each other, we cannot remove lazy imports and make the direction of dependency completely one-way. Still, there are parts we can organize.

For example, typeinfo depends on automation, but it should be the other way around. We will sort out this situation to make future feature development and debugging easier.

Changes

1. Split automation.py into _automation.py and automation.py

  • Created _automation.py containing the low-level automation types (VARIANT, tagCY, tagDEC, VARTYPE, DISPID, etc.) and helper functions (_ctype_to_vartype, _vartype_to_ctype, _arraycode_to_vartype, fixed-point number marshaling helpers).
  • automation.py becomes a thin re-export layer over _automation.py for backward compatibility.
  • typeinfo.py now imports these from _automation instead of from automation.

Rationale: typeinfo was importing from automation, creating a dependency direction that is backwards from what the type hierarchy would suggest. By splitting automation into a low-level _automation (no internal deps) and a thin automation re-export layer, typeinfo can depend on _automation directly, avoiding circular imports.

2. Split typeinfo.py into recordinfo.py and typeinfo.py

  • Created recordinfo.py containing:
    • IRecordInfo interface
    • GetRecordInfoFromTypeInfo
    • GetRecordInfoFromGuids
    • SafeArrayGetRecordInfo
  • Removed these from typeinfo.py, replacing with re-exports from recordinfo.

Rationale: IRecordInfo and its factory functions were previously bundled inside typeinfo.py. Extracting them into a dedicated recordinfo module makes the dependency graph clearer and reduces the surface area of typeinfo.

3. Remove lazy imports from safearray.py

  • All from comtypes.automation import ... and from comtypes.typeinfo import ... inside functions in safearray.py have been moved to module-level imports at the bottom of the file.
  • safearray.py now imports from comtypes._automation, comtypes.automation, and comtypes.recordinfo directly.

Rationale: Lazy imports inside functions were a workaround for circular dependencies. With the module reorganization, these circular dependencies are resolved, so the imports can be made at module level. This makes the code easier to read, debug, and analyze statically.

4. Move tagSAFEARRAYBOUND to _safearray.py

  • tagSAFEARRAYBOUND and SAFEARRAYBOUND are now defined in _safearray.py and re-imported by typeinfo.py.

Rationale: These types belong with the SAFEARRAY infrastructure, not with the typeinfo module.

Dependency Graph (After)

IUnknown, GUID, HRESULT, BSTR, COMMETHOD, GUID, ...
        ↑
_safearray.py    (no lazy imports)
        ↑
_automation.py   (lazy: safearray, automation, recordinfo)
        ↑
typeinfo.py      (lazy: recordinfo, _safearray)
recordinfo.py    (no lazy imports)
        ↑
safearray.py     (lazy: _automation, automation, recordinfo)
        ↑
automation.py    (re-exports from _automation)

@junkmd junkmd added coding style Topics related to style, formatters, linters, and delinting. debt labels Nov 12, 2026
@codecov-commenter

codecov-commenter commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.80%. Comparing base (b4bfab5) to head (b199248).

Additional details and impacted files
@@                    Coverage Diff                     @@
##           safearray_typeinfo_dep     #987      +/-   ##
==========================================================
+ Coverage                   87.16%   90.80%   +3.64%     
==========================================================
  Files                         149      149              
  Lines                       15993    15466     -527     
==========================================================
+ Hits                        13940    14044     +104     
+ Misses                       2053     1422     -631     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@junkmd
junkmd changed the base branch from main to safearray_typeinfo_dep November 12, 2026 00:33
@junkmd
junkmd added this pull request to stack #992 November 12, 2026 00:33
@junkmd
junkmd merged commit 1dc7ab2 into main Nov 12, 2026
440 of 452 checks passed
junkmd added a commit that referenced this pull request Nov 12, 2026
* chore: Copy `typeinfo.py` to keep git histories.

* chore: Copy `automation.py` to keep git histories.
junkmd added a commit that referenced this pull request Nov 12, 2026
* Refactor: Move `IRecordInfo` and related functions to `recordinfo` module.

* Refactor: Move `SafeArrayGetRecordInfo` to `recordinfo` module and update references.

* Refactor: Update import statements to move `GetRecordInfoFromGuids` and `IRecordInfo` from `typeinfo` to `recordinfo` module.
@junkmd
junkmd deleted the typeinfo_automation_dep_dir branch November 12, 2026 02:23
@junkmd junkmd added this to the 1.5.1 milestone Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coding style Topics related to style, formatters, linters, and delinting. debt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants