Improve type hints for generated properties.#889
Merged
junkmd merged 6 commits intoenthought:mainfrom Dec 22, 2025
Merged
Conversation
Adds two new tests, `test_valid_syntax_dispmethods` and `test_valid_syntax_commethods`, to `test_typeannotator.py`. These tests verify that the code generated by `DispInterfaceMembersAnnotator` and `ComInterfaceMembersAnnotator` is syntactically valid Python. They do this by using `ast.parse` on the generated code, which will raise an exception if the syntax is incorrect.
The previous logic in `ComMethodAnnotator` and `DispMethodAnnotator` generated a generic `**kwargs: hints.Any` signature for methods where a required parameter followed an optional one. This pattern is typical for named property assignments (e.g., `obj.prop[key] = value`), but the resulting signature was imprecise for static analysis. This commit refines the signature generation for these cases. It now produces a more specific signature using a positional-only marker and variadic arguments, like `/, *args: hints.Unpack[tuple[...]]`. This change provides more accurate type hints for multi-argument `propput` and `propputref` operations, enhancing type safety for both COM and dispatch interfaces.
The `_generate_trailing_params` function handles a specific edge case in COM interface generation: `propput` or `propputref` methods with multiple arguments.
This commit significantly enhances the tests for `ComMethodAnnotator` and `DispMethodAnnotator` to cover more complex argument patterns and improves the underlying signature generation logic.
This commit expands the test suite in `test_typeannotator.py` to cover `propput` properties that accept arguments. The test for `ComInterfaceMembersAnnotator` now includes a `ham` property with a setter that takes an argument. This ensures that the generated type hint correctly reflects the method signature for such setters, which was not previously tested.
…in `hints.pyi`. Apply '/' marker to descriptor protocol and dunder methods, improving clarity and compliance with PEP 570.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #889 +/- ##
==========================================
+ Coverage 85.00% 85.02% +0.02%
==========================================
Files 126 126
Lines 11699 11716 +17
==========================================
+ Hits 9945 9962 +17
Misses 1754 1754 ☔ View full report in Codecov by Sentry. 🚀 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.
This change improves the accuracy and PEP 570 compliance of type hints generated by
comtypes.Specifically, it refines the signature generation for named properties, especially those taking multiple arguments (like
propputandpropputref).It also adds syntax validation tests for generated interface members.
Note
The following code is an excerpt from the definitions of the
Rangeproperty of_Worksheetand theValueproperty ofRangein Excel.With this implementation, Pylance can successfully resolve the type definitions and display them in the hover tooltip.