Repository navigation
Make type dispatch and interface modeling statically safer in tlbparser and codegenerator. - #975
Merged
Merged
Conversation
…erate`. Use type narrowing after `generate` receives an item so `name` can be retrieved safely without relying on `getattr`.
Use explicit type checks for types and container element types that `tlbparser` can produce instead of dynamically retrieving type names and calling methods via `getattr`, improving type safety. Confirm that paths reaching an error without calling any method are inferred as `Never`.
Share the type-based dispatch logic between `generate` and `_define_interface` so all supported type descriptions use the same generation path.
Replace exact `type(...)` comparisons in `CodeGenerator` with `isinstance` checks. All `typedesc` produced by `tlbparser` and used by `CodeGenerator` are marked with `typing.final`, so the accepted concrete types cannot have subclasses. This preserves the previous exact-type guarantee while allowing `isinstance` to avoid E721 violations. Types that `tlbparser` does not produce, including skipped function-definition types, are intentionally not marked `final`.
Define `IUnknown` and `IDispatch` with the practical COM interface members needed by their use cases, while keeping the models intentionally simplified instead of reproducing every detail from `stdole`. Remove the `Typedef[None]` alias and related special handling now that `Typedef` no longer contains `None`.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #975 +/- ##
==========================================
+ Coverage 89.57% 89.61% +0.04%
==========================================
Files 143 143
Lines 14175 14247 +72
==========================================
+ Hits 12697 12768 +71
- Misses 1478 1479 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CodeGeneratortype annotations and centralize explicit dispatchfinalisinstanceIUnknownandIDispatchas simplifiedComInterfacedefinitions intlbparserTypedef[None]alias and related special handlingBackground and purpose
CodeGeneratorpreviously obtained type names dynamically and invoked generation methods throughgetattr. As a result, the types accepted as input and their corresponding handlers were not clearly visible to the type system.By explicitly annotating the types accepted by
generateand dispatching supported types explicitly, invalid type handling can be detected earlier.The dispatch logic was also duplicated across multiple paths, including interface generation.
Centralizing it ensures that the same type is handled consistently regardless of the path used, making the code-generation flow easier to follow and reducing the risk of missing updates when types are added or changed.
The existing
type(...) is ...comparisons triggered E721 violations in static analysis. Replacing them directly withisinstancewould potentially allow subclasses and change the intended behavior.Marking the concrete types produced by
tlbparserand handled byCodeGeneratorasfinalpreserves the original intent of accepting only the supported concrete types while allowingisinstanceto be used safely.tlbparserpreviously representedIUnknownandIDispatchas empty typedefs. This did not reflect the interfaces used by the generated type descriptions.Defining simplified
ComInterfacemodels with the practical COM members required by their use cases makes the mapping more accurate without attempting to reproduce every detail fromstdole.Because
Typedefno longer containsNone, theTypedef[None]alias and its related special handling also be removed.Verification points
tlbparseris dispatched to the correct generation methodTypeErrorinstead of being invoked implicitlyIUnknownandIDispatchare mapped to the expected practical interface definitionsTypedefno longer requires support forNone