chore: Add typed stub for twilio.rest.Client + to_dict type hints fix by al-chris · Pull Request #899 · twilio/twilio-python
Fixes
Summary
- Added a type stub exposing the
Clientinitializer and domain properties:twilio/rest/__init__.pyi. - Included a follow-up runtime fix that adds return type hints to
to_dict()methods and safer policy handling in the Knowledge module.
Commits included
914845712feat: add initial typings for Twilio REST cliente70d11facfix: add type hints and improve policy handling in to_dict methods
Files changed
- A new stub:
twilio/rest/__init__.pyi - Modified runtime module:
twilio/rest/knowledge/v1/knowledge/__init__.py(type hints and policy handling in multipleto_dictmethods)
Why
- The
.pyistub improves IDE/autocomplete and prepares the codebase for static typing. - The runtime changes ensure
to_dict()is correctly typed and avoid attribute errors whenpolicyobjects do not implementto_dict().
Patch summary for knowledge module (selected excerpt)
Commit: e70d11f
Message: fix: add type hints and improve policy handling in to_dict methods
Diff excerpt:
--- a/twilio/rest/knowledge/v1/knowledge/init.py
+++ b/twilio/rest/knowledge/v1/knowledge/init.py
@@
-
def to_dict(self) -> Dict[str, Any]: return { "description": self.description, "knowledge_source_details": self.knowledge_source_details, "name": self.name,
-
"policy": self.policy.to_dict() if self.policy is not None else None,
-
"policy": self.policy.to_dict() if (self.policy is not None and hasattr(self.policy, "to_dict")) else None, "type": self.type, "embedding_model": self.embedding_model, }
(Repeated in multiple classes: KnowledgeInstance, KnowledgeContext, KnowledgeList — each to_dict now has a -> Dict[str, Any] return annotation and safer policy handling.)
How to test
- Run type checks and tests locally:
pip install -e .[dev] mypy twilio --follow-imports=silent pytest -q
Notes
- The
.pyifile is annotation-only and doesn't change runtime behavior. - The knowledge module changes are small, well-scoped runtime fixes; they should be safe but please run unit tests and smoke test features that use
policyobjects.
Suggested branch/commit
- Branch:
add/stubs/twilio-rest-client - Commit messages already present in history:
914845712,e70d11fac
Requesting review from
- @maintainers or module owners familiar with
twilio.restandknowledgemodules
Checklist
- I acknowledge that all my contributions will be made under the project's license
- I have made a material change to the repo (functionality, testing, spelling, grammar)
- I have read the Contribution Guidelines and my PR follows them
- I have titled the PR appropriately
- I have updated my branch with the main branch
- I have added tests that prove my fix is effective or that my feature works
- I have added the necessary documentation about the functionality in the appropriate .md file
- I have added inline documentation to the code I modified
If you have questions, please file a support ticket, or create a GitHub Issue in this repository.