fix: enhance IntelliSense support by adding type hints for Prompt att…#28
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances IntelliSense support for prompt context managers by adding comprehensive type hints to PromptContextManager and AsyncPromptContextManager classes. The changes improve developer experience by enabling better IDE autocomplete and type checking for attributes that are dynamically forwarded from the wrapped Prompt object.
Key Changes:
- Added type hint class attributes for forwarded
Promptattributes to enable IntelliSense - Added return type annotations to all context manager methods (
__init__,__enter__,__exit__,__aenter__,__aexit__,__getattr__,__repr__,__str__) - Added explicit type annotations to instance variables in
__init__methods - Added
compile_variablesmethod to both context managers to explicitly forward this method from the wrappedPrompt - Added type hints to the
_PromptContextMixinclass for shared attributes
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def compile_variables(self, variables: dict[str, Any]) -> Prompt: | ||
| """Forward compile_variables to wrapped Prompt.""" | ||
| return self._prompt.compile_variables(variables) |
There was a problem hiding this comment.
The compile_variables method was added to AsyncPromptContextManager but there's no test coverage for it. Consider adding a test similar to test_wrapper_forwards_all_prompt_methods in tests/prompts/test_context_manager.py but for the async version to ensure this method is properly tested.
| self._tag: str | None = tag | ||
| self._variables: dict[str, Any] | None = variables | ||
| self._from_cache: bool = from_cache | ||
| self._context_token: Any = None |
There was a problem hiding this comment.
The type hint for _context_token could be more specific. Since _current_prompt_context.set() returns a Token object from the contextvars module, consider changing the type from Any to contextvars.Token[dict[str, Any] | None] | None for better type safety. You'll need to import Token from contextvars.
| self._tag: str | None = tag | ||
| self._variables: dict[str, Any] | None = variables | ||
| self._from_cache: bool = from_cache | ||
| self._context_token: Any = None |
There was a problem hiding this comment.
The type hint for _context_token could be more specific. Since _current_prompt_context.set() returns a Token object from the contextvars module, consider changing the type from Any to contextvars.Token[dict[str, Any] | None] | None for better type safety. You'll need to import Token from contextvars.
…ributes