Python lint: Ruff rules for comprehensions and performance (#512)

This commit is contained in:
Christian Clauss
2025-04-15 16:58:33 +02:00
committed by GitHub
parent 8c9269c34b
commit babb477dff
6 changed files with 15 additions and 14 deletions

View File

@@ -122,8 +122,10 @@ class Server:
for item in tools_response: for item in tools_response:
if isinstance(item, tuple) and item[0] == "tools": if isinstance(item, tuple) and item[0] == "tools":
for tool in item[1]: tools.extend(
tools.append(Tool(tool.name, tool.description, tool.inputSchema)) Tool(tool.name, tool.description, tool.inputSchema)
for tool in item[1]
)
return tools return tools
@@ -282,10 +284,9 @@ class ChatSession:
async def cleanup_servers(self) -> None: async def cleanup_servers(self) -> None:
"""Clean up all servers properly.""" """Clean up all servers properly."""
cleanup_tasks = [] cleanup_tasks = [
for server in self.servers: asyncio.create_task(server.cleanup()) for server in self.servers
cleanup_tasks.append(asyncio.create_task(server.cleanup())) ]
if cleanup_tasks: if cleanup_tasks:
try: try:
await asyncio.gather(*cleanup_tasks, return_exceptions=True) await asyncio.gather(*cleanup_tasks, return_exceptions=True)

View File

@@ -89,8 +89,8 @@ venv = ".venv"
strict = ["src/mcp/**/*.py"] strict = ["src/mcp/**/*.py"]
[tool.ruff.lint] [tool.ruff.lint]
select = ["E", "F", "I", "UP"] select = ["C4", "E", "F", "I", "PERF", "UP"]
ignore = [] ignore = ["PERF203"]
[tool.ruff] [tool.ruff]
line-length = 88 line-length = 88

View File

@@ -80,7 +80,7 @@ class FuncMetadata(BaseModel):
dicts (JSON objects) as JSON strings, which can be pre-parsed here. dicts (JSON objects) as JSON strings, which can be pre-parsed here.
""" """
new_data = data.copy() # Shallow copy new_data = data.copy() # Shallow copy
for field_name, _field_info in self.arg_model.model_fields.items(): for field_name in self.arg_model.model_fields.keys():
if field_name not in data.keys(): if field_name not in data.keys():
continue continue
if isinstance(data[field_name], str): if isinstance(data[field_name], str):

View File

@@ -42,7 +42,7 @@ async def test_server_base64_encoding_issue():
# Create binary data that will definitely result in + and / characters # Create binary data that will definitely result in + and / characters
# when encoded with standard base64 # when encoded with standard base64
binary_data = bytes([x for x in range(255)] * 4) binary_data = bytes(list(range(255)) * 4)
# Register a resource handler that returns our test data # Register a resource handler that returns our test data
@server.read_resource() @server.read_resource()

View File

@@ -38,7 +38,7 @@ class TestRenderPrompt:
return f"Hello, {name}! You're {age} years old." return f"Hello, {name}! You're {age} years old."
prompt = Prompt.from_function(fn) prompt = Prompt.from_function(fn)
assert await prompt.render(arguments=dict(name="World")) == [ assert await prompt.render(arguments={"name": "World"}) == [
UserMessage( UserMessage(
content=TextContent( content=TextContent(
type="text", text="Hello, World! You're 30 years old." type="text", text="Hello, World! You're 30 years old."
@@ -53,7 +53,7 @@ class TestRenderPrompt:
prompt = Prompt.from_function(fn) prompt = Prompt.from_function(fn)
with pytest.raises(ValueError): with pytest.raises(ValueError):
await prompt.render(arguments=dict(age=40)) await prompt.render(arguments={"age": 40})
@pytest.mark.anyio @pytest.mark.anyio
async def test_fn_returns_message(self): async def test_fn_returns_message(self):

View File

@@ -115,7 +115,7 @@ async def test_read_resource_file(mcp: FastMCP):
@pytest.mark.anyio @pytest.mark.anyio
async def test_delete_file(mcp: FastMCP, test_dir: Path): async def test_delete_file(mcp: FastMCP, test_dir: Path):
await mcp.call_tool( await mcp.call_tool(
"delete_file", arguments=dict(path=str(test_dir / "example.py")) "delete_file", arguments={"path": str(test_dir / "example.py")}
) )
assert not (test_dir / "example.py").exists() assert not (test_dir / "example.py").exists()
@@ -123,7 +123,7 @@ async def test_delete_file(mcp: FastMCP, test_dir: Path):
@pytest.mark.anyio @pytest.mark.anyio
async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path): async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):
await mcp.call_tool( await mcp.call_tool(
"delete_file", arguments=dict(path=str(test_dir / "example.py")) "delete_file", arguments={"path": str(test_dir / "example.py")}
) )
res_iter = await mcp.read_resource("file://test_dir/example.py") res_iter = await mcp.read_resource("file://test_dir/example.py")
res_list = list(res_iter) res_list = list(res_iter)