|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | from collections.abc import AsyncIterable, AsyncIterator |
| 5 | +from typing import Any |
5 | 6 |
|
6 | 7 | from ._errors import CLIConnectionError |
7 | 8 | from .types import ClaudeCodeOptions, Message, ResultMessage |
@@ -94,19 +95,20 @@ def __init__(self, options: ClaudeCodeOptions | None = None): |
94 | 95 | if options is None: |
95 | 96 | options = ClaudeCodeOptions() |
96 | 97 | self.options = options |
97 | | - self._transport = None |
| 98 | + self._transport: Any | None = None |
98 | 99 | os.environ["CLAUDE_CODE_ENTRYPOINT"] = "sdk-py-client" |
99 | 100 |
|
100 | | - async def connect(self, prompt: str | AsyncIterable[dict] | None = None) -> None: |
| 101 | + async def connect(self, prompt: str | AsyncIterable[dict[str, Any]] | None = None) -> None: |
101 | 102 | """Connect to Claude with a prompt or message stream.""" |
102 | 103 | from ._internal.transport.subprocess_cli import SubprocessCLITransport |
103 | 104 |
|
104 | 105 | # Auto-connect with empty async iterable if no prompt is provided |
105 | | - async def _empty_stream(): |
| 106 | + async def _empty_stream() -> AsyncIterator[dict[str, Any]]: |
106 | 107 | # Never yields, but indicates that this function is an iterator and |
107 | 108 | # keeps the connection open. |
108 | | - if False: |
109 | | - yield |
| 109 | + # This yield is never reached but makes this an async generator |
| 110 | + return |
| 111 | + yield {} # type: ignore[unreachable] |
110 | 112 |
|
111 | 113 | self._transport = SubprocessCLITransport( |
112 | 114 | prompt=_empty_stream() if prompt is None else prompt, |
@@ -190,12 +192,12 @@ async def disconnect(self) -> None: |
190 | 192 | await self._transport.disconnect() |
191 | 193 | self._transport = None |
192 | 194 |
|
193 | | - async def __aenter__(self): |
| 195 | + async def __aenter__(self) -> "ClaudeSDKClient": |
194 | 196 | """Enter async context - automatically connects with empty stream for interactive use.""" |
195 | 197 | await self.connect() |
196 | 198 | return self |
197 | 199 |
|
198 | | - async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 200 | + async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool: |
199 | 201 | """Exit async context - always disconnects.""" |
200 | 202 | await self.disconnect() |
201 | 203 | return False |
0 commit comments