Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 63ef121

Browse files
committed
Fix code formatting for CI
1 parent 6ca3514 commit 63ef121

File tree

8 files changed

+101
-79
lines changed

8 files changed

+101
-79
lines changed

examples/quick_start.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ async def with_options_example():
3434
)
3535

3636
async for message in query(
37-
prompt="Explain what Python is in one sentence.",
38-
options=options
37+
prompt="Explain what Python is in one sentence.", options=options
3938
):
4039
if isinstance(message, AssistantMessage):
4140
for block in message.content:
@@ -55,7 +54,7 @@ async def with_tools_example():
5554

5655
async for message in query(
5756
prompt="Create a file called hello.txt with 'Hello, World!' in it",
58-
options=options
57+
options=options,
5958
):
6059
if isinstance(message, AssistantMessage):
6160
for block in message.content:

src/claude_code_sdk/_internal/client.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def _parse_message(self, data: dict[str, Any]) -> Message | None:
4848
match data["type"]:
4949
case "user":
5050
# Extract just the content from the nested structure
51-
return UserMessage(
52-
content=data["message"]["content"]
53-
)
51+
return UserMessage(content=data["message"]["content"])
5452

5553
case "assistant":
5654
# Parse content blocks
@@ -60,24 +58,28 @@ def _parse_message(self, data: dict[str, Any]) -> Message | None:
6058
case "text":
6159
content_blocks.append(TextBlock(text=block["text"]))
6260
case "tool_use":
63-
content_blocks.append(ToolUseBlock(
64-
id=block["id"],
65-
name=block["name"],
66-
input=block["input"]
67-
))
61+
content_blocks.append(
62+
ToolUseBlock(
63+
id=block["id"],
64+
name=block["name"],
65+
input=block["input"],
66+
)
67+
)
6868
case "tool_result":
69-
content_blocks.append(ToolResultBlock(
70-
tool_use_id=block["tool_use_id"],
71-
content=block.get("content"),
72-
is_error=block.get("is_error")
73-
))
69+
content_blocks.append(
70+
ToolResultBlock(
71+
tool_use_id=block["tool_use_id"],
72+
content=block.get("content"),
73+
is_error=block.get("is_error"),
74+
)
75+
)
7476

7577
return AssistantMessage(content=content_blocks)
7678

7779
case "system":
7880
return SystemMessage(
7981
subtype=data["subtype"],
80-
data=data # Pass through all data
82+
data=data, # Pass through all data
8183
)
8284

8385
case "result":
@@ -92,7 +94,7 @@ def _parse_message(self, data: dict[str, Any]) -> Message | None:
9294
session_id=data["session_id"],
9395
total_cost_usd=data["total_cost"],
9496
usage=data.get("usage"),
95-
result=data.get("result")
97+
result=data.get("result"),
9698
)
9799

98100
case _:

src/claude_code_sdk/_internal/transport/subprocess_cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class SubprocessCLITransport(Transport):
2222
"""Subprocess transport using Claude Code CLI."""
2323

2424
def __init__(
25-
self, prompt: str, options: ClaudeCodeOptions, cli_path: str | Path | None = None
25+
self,
26+
prompt: str,
27+
options: ClaudeCodeOptions,
28+
cli_path: str | Path | None = None,
2629
):
2730
self._prompt = prompt
2831
self._options = options

src/claude_code_sdk/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# MCP Server config
1414
class McpServerConfig(TypedDict):
1515
"""MCP server configuration."""
16+
1617
transport: list[str]
1718
env: NotRequired[dict[str, Any]]
1819

@@ -21,12 +22,14 @@ class McpServerConfig(TypedDict):
2122
@dataclass
2223
class TextBlock:
2324
"""Text content block."""
25+
2426
text: str
2527

2628

2729
@dataclass
2830
class ToolUseBlock:
2931
"""Tool use content block."""
32+
3033
id: str
3134
name: str
3235
input: dict[str, Any]
@@ -35,6 +38,7 @@ class ToolUseBlock:
3538
@dataclass
3639
class ToolResultBlock:
3740
"""Tool result content block."""
41+
3842
tool_use_id: str
3943
content: str | list[dict[str, Any]] | None = None
4044
is_error: bool | None = None
@@ -47,25 +51,29 @@ class ToolResultBlock:
4751
@dataclass
4852
class UserMessage:
4953
"""User message."""
54+
5055
content: str
5156

5257

5358
@dataclass
5459
class AssistantMessage:
5560
"""Assistant message with content blocks."""
61+
5662
content: list[ContentBlock]
5763

5864

5965
@dataclass
6066
class SystemMessage:
6167
"""System message with metadata."""
68+
6269
subtype: str
6370
data: dict[str, Any]
6471

6572

6673
@dataclass
6774
class ResultMessage:
6875
"""Result message with cost and usage information."""
76+
6977
subtype: str
7078
cost_usd: float
7179
duration_ms: int
@@ -84,6 +92,7 @@ class ResultMessage:
8492
@dataclass
8593
class ClaudeCodeOptions:
8694
"""Query options for Claude SDK."""
95+
8796
allowed_tools: list[str] = field(default_factory=list)
8897
max_thinking_tokens: int = 8000
8998
system_prompt: str | None = None

tests/test_client.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ class TestQueryFunction:
1313

1414
def test_query_single_prompt(self):
1515
"""Test query with a single prompt."""
16+
1617
async def _test():
17-
with patch('claude_code_sdk._internal.client.InternalClient.process_query') as mock_process:
18+
with patch(
19+
"claude_code_sdk._internal.client.InternalClient.process_query"
20+
) as mock_process:
1821
# Mock the async generator
1922
async def mock_generator():
20-
yield AssistantMessage(
21-
content=[TextBlock(text="4")]
22-
)
23+
yield AssistantMessage(content=[TextBlock(text="4")])
2324

2425
mock_process.return_value = mock_generator()
2526

@@ -35,41 +36,43 @@ async def mock_generator():
3536

3637
def test_query_with_options(self):
3738
"""Test query with various options."""
39+
3840
async def _test():
39-
with patch('claude_code_sdk._internal.client.InternalClient.process_query') as mock_process:
41+
with patch(
42+
"claude_code_sdk._internal.client.InternalClient.process_query"
43+
) as mock_process:
44+
4045
async def mock_generator():
41-
yield AssistantMessage(
42-
content=[TextBlock(text="Hello!")]
43-
)
46+
yield AssistantMessage(content=[TextBlock(text="Hello!")])
4447

4548
mock_process.return_value = mock_generator()
4649

4750
options = ClaudeCodeOptions(
4851
allowed_tools=["Read", "Write"],
4952
system_prompt="You are helpful",
5053
permission_mode="acceptEdits",
51-
max_turns=5
54+
max_turns=5,
5255
)
5356

5457
messages = []
55-
async for msg in query(
56-
prompt="Hi",
57-
options=options
58-
):
58+
async for msg in query(prompt="Hi", options=options):
5959
messages.append(msg)
6060

6161
# Verify process_query was called with correct prompt and options
6262
mock_process.assert_called_once()
6363
call_args = mock_process.call_args
64-
assert call_args[1]['prompt'] == "Hi"
65-
assert call_args[1]['options'] == options
64+
assert call_args[1]["prompt"] == "Hi"
65+
assert call_args[1]["options"] == options
6666

6767
anyio.run(_test)
6868

6969
def test_query_with_cwd(self):
7070
"""Test query with custom working directory."""
71+
7172
async def _test():
72-
with patch('claude_code_sdk._internal.client.SubprocessCLITransport') as mock_transport_class:
73+
with patch(
74+
"claude_code_sdk._internal.client.SubprocessCLITransport"
75+
) as mock_transport_class:
7376
mock_transport = AsyncMock()
7477
mock_transport_class.return_value = mock_transport
7578

@@ -79,8 +82,8 @@ async def mock_receive():
7982
"type": "assistant",
8083
"message": {
8184
"role": "assistant",
82-
"content": [{"type": "text", "text": "Done"}]
83-
}
85+
"content": [{"type": "text", "text": "Done"}],
86+
},
8487
}
8588
yield {
8689
"type": "result",
@@ -91,7 +94,7 @@ async def mock_receive():
9194
"is_error": False,
9295
"num_turns": 1,
9396
"session_id": "test-session",
94-
"total_cost": 0.001
97+
"total_cost": 0.001,
9598
}
9699

97100
mock_transport.receive_messages = mock_receive
@@ -100,16 +103,13 @@ async def mock_receive():
100103

101104
options = ClaudeCodeOptions(cwd="/custom/path")
102105
messages = []
103-
async for msg in query(
104-
prompt="test",
105-
options=options
106-
):
106+
async for msg in query(prompt="test", options=options):
107107
messages.append(msg)
108108

109109
# Verify transport was created with correct parameters
110110
mock_transport_class.assert_called_once()
111111
call_kwargs = mock_transport_class.call_args.kwargs
112-
assert call_kwargs['prompt'] == "test"
113-
assert call_kwargs['options'].cwd == "/custom/path"
112+
assert call_kwargs["prompt"] == "test"
113+
assert call_kwargs["options"].cwd == "/custom/path"
114114

115115
anyio.run(_test)

tests/test_integration.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ class TestIntegration:
2323

2424
def test_simple_query_response(self):
2525
"""Test a simple query with text response."""
26+
2627
async def _test():
27-
with patch("claude_code_sdk._internal.client.SubprocessCLITransport") as mock_transport_class:
28+
with patch(
29+
"claude_code_sdk._internal.client.SubprocessCLITransport"
30+
) as mock_transport_class:
2831
mock_transport = AsyncMock()
2932
mock_transport_class.return_value = mock_transport
3033

@@ -71,12 +74,15 @@ async def mock_receive():
7174
assert messages[1].cost_usd == 0.001
7275
assert messages[1].session_id == "test-session"
7376

74-
7577
anyio.run(_test)
78+
7679
def test_query_with_tool_use(self):
7780
"""Test query that uses tools."""
81+
7882
async def _test():
79-
with patch("claude_code_sdk._internal.client.SubprocessCLITransport") as mock_transport_class:
83+
with patch(
84+
"claude_code_sdk._internal.client.SubprocessCLITransport"
85+
) as mock_transport_class:
8086
mock_transport = AsyncMock()
8187
mock_transport_class.return_value = mock_transport
8288

@@ -135,25 +141,31 @@ async def mock_receive():
135141
assert messages[0].content[1].name == "Read"
136142
assert messages[0].content[1].input["file_path"] == "/test.txt"
137143

138-
139144
anyio.run(_test)
145+
140146
def test_cli_not_found(self):
141147
"""Test handling when CLI is not found."""
148+
142149
async def _test():
143-
with patch("shutil.which", return_value=None), patch(
144-
"pathlib.Path.exists", return_value=False
145-
), pytest.raises(CLINotFoundError) as exc_info:
150+
with (
151+
patch("shutil.which", return_value=None),
152+
patch("pathlib.Path.exists", return_value=False),
153+
pytest.raises(CLINotFoundError) as exc_info,
154+
):
146155
async for _ in query(prompt="test"):
147156
pass
148157

149158
assert "Claude Code requires Node.js" in str(exc_info.value)
150159

151-
152160
anyio.run(_test)
161+
153162
def test_continuation_option(self):
154163
"""Test query with continue_conversation option."""
164+
155165
async def _test():
156-
with patch("claude_code_sdk._internal.client.SubprocessCLITransport") as mock_transport_class:
166+
with patch(
167+
"claude_code_sdk._internal.client.SubprocessCLITransport"
168+
) as mock_transport_class:
157169
mock_transport = AsyncMock()
158170
mock_transport_class.return_value = mock_transport
159171

@@ -179,13 +191,14 @@ async def mock_receive():
179191
# Run query with continuation
180192
messages = []
181193
async for msg in query(
182-
prompt="Continue", options=ClaudeCodeOptions(continue_conversation=True)
194+
prompt="Continue",
195+
options=ClaudeCodeOptions(continue_conversation=True),
183196
):
184197
messages.append(msg)
185198

186199
# Verify transport was created with continuation option
187200
mock_transport_class.assert_called_once()
188201
call_kwargs = mock_transport_class.call_args.kwargs
189-
assert call_kwargs['options'].continue_conversation is True
202+
assert call_kwargs["options"].continue_conversation is True
190203

191204
anyio.run(_test)

0 commit comments

Comments
 (0)