Skip to content

Commit babb477

Browse files
authored
Python lint: Ruff rules for comprehensions and performance (#512)
1 parent 8c9269c commit babb477

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

Diff for: examples/clients/simple-chatbot/mcp_simple_chatbot/main.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ async def list_tools(self) -> list[Any]:
122122

123123
for item in tools_response:
124124
if isinstance(item, tuple) and item[0] == "tools":
125-
for tool in item[1]:
126-
tools.append(Tool(tool.name, tool.description, tool.inputSchema))
125+
tools.extend(
126+
Tool(tool.name, tool.description, tool.inputSchema)
127+
for tool in item[1]
128+
)
127129

128130
return tools
129131

@@ -282,10 +284,9 @@ def __init__(self, servers: list[Server], llm_client: LLMClient) -> None:
282284

283285
async def cleanup_servers(self) -> None:
284286
"""Clean up all servers properly."""
285-
cleanup_tasks = []
286-
for server in self.servers:
287-
cleanup_tasks.append(asyncio.create_task(server.cleanup()))
288-
287+
cleanup_tasks = [
288+
asyncio.create_task(server.cleanup()) for server in self.servers
289+
]
289290
if cleanup_tasks:
290291
try:
291292
await asyncio.gather(*cleanup_tasks, return_exceptions=True)

Diff for: pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ venv = ".venv"
8989
strict = ["src/mcp/**/*.py"]
9090

9191
[tool.ruff.lint]
92-
select = ["E", "F", "I", "UP"]
93-
ignore = []
92+
select = ["C4", "E", "F", "I", "PERF", "UP"]
93+
ignore = ["PERF203"]
9494

9595
[tool.ruff]
9696
line-length = 88

Diff for: src/mcp/server/fastmcp/utilities/func_metadata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def pre_parse_json(self, data: dict[str, Any]) -> dict[str, Any]:
8080
dicts (JSON objects) as JSON strings, which can be pre-parsed here.
8181
"""
8282
new_data = data.copy() # Shallow copy
83-
for field_name, _field_info in self.arg_model.model_fields.items():
83+
for field_name in self.arg_model.model_fields.keys():
8484
if field_name not in data.keys():
8585
continue
8686
if isinstance(data[field_name], str):

Diff for: tests/issues/test_342_base64_encoding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def test_server_base64_encoding_issue():
4242

4343
# Create binary data that will definitely result in + and / characters
4444
# when encoded with standard base64
45-
binary_data = bytes([x for x in range(255)] * 4)
45+
binary_data = bytes(list(range(255)) * 4)
4646

4747
# Register a resource handler that returns our test data
4848
@server.read_resource()

Diff for: tests/server/fastmcp/prompts/test_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def fn(name: str, age: int = 30) -> str:
3838
return f"Hello, {name}! You're {age} years old."
3939

4040
prompt = Prompt.from_function(fn)
41-
assert await prompt.render(arguments=dict(name="World")) == [
41+
assert await prompt.render(arguments={"name": "World"}) == [
4242
UserMessage(
4343
content=TextContent(
4444
type="text", text="Hello, World! You're 30 years old."
@@ -53,7 +53,7 @@ async def fn(name: str, age: int = 30) -> str:
5353

5454
prompt = Prompt.from_function(fn)
5555
with pytest.raises(ValueError):
56-
await prompt.render(arguments=dict(age=40))
56+
await prompt.render(arguments={"age": 40})
5757

5858
@pytest.mark.anyio
5959
async def test_fn_returns_message(self):

Diff for: tests/server/fastmcp/servers/test_file_server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ async def test_read_resource_file(mcp: FastMCP):
115115
@pytest.mark.anyio
116116
async def test_delete_file(mcp: FastMCP, test_dir: Path):
117117
await mcp.call_tool(
118-
"delete_file", arguments=dict(path=str(test_dir / "example.py"))
118+
"delete_file", arguments={"path": str(test_dir / "example.py")}
119119
)
120120
assert not (test_dir / "example.py").exists()
121121

122122

123123
@pytest.mark.anyio
124124
async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):
125125
await mcp.call_tool(
126-
"delete_file", arguments=dict(path=str(test_dir / "example.py"))
126+
"delete_file", arguments={"path": str(test_dir / "example.py")}
127127
)
128128
res_iter = await mcp.read_resource("file://test_dir/example.py")
129129
res_list = list(res_iter)

0 commit comments

Comments
 (0)