Skip to content

Python lint: Ruff rules for comprehensions and performance #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions examples/clients/simple-chatbot/mcp_simple_chatbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ async def list_tools(self) -> list[Any]:

for item in tools_response:
if isinstance(item, tuple) and item[0] == "tools":
for tool in item[1]:
tools.append(Tool(tool.name, tool.description, tool.inputSchema))
tools.extend(
Tool(tool.name, tool.description, tool.inputSchema)
for tool in item[1]
)

return tools

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

async def cleanup_servers(self) -> None:
"""Clean up all servers properly."""
cleanup_tasks = []
for server in self.servers:
cleanup_tasks.append(asyncio.create_task(server.cleanup()))

cleanup_tasks = [
asyncio.create_task(server.cleanup()) for server in self.servers
]
if cleanup_tasks:
try:
await asyncio.gather(*cleanup_tasks, return_exceptions=True)
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ venv = ".venv"
strict = ["src/mcp/**/*.py"]

[tool.ruff.lint]
select = ["E", "F", "I", "UP"]
ignore = []
select = ["C4", "E", "F", "I", "PERF", "UP"]
ignore = ["PERF203"]

[tool.ruff]
line-length = 88
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/server/fastmcp/utilities/func_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def pre_parse_json(self, data: dict[str, Any]) -> dict[str, Any]:
dicts (JSON objects) as JSON strings, which can be pre-parsed here.
"""
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():
continue
if isinstance(data[field_name], str):
Expand Down
2 changes: 1 addition & 1 deletion tests/issues/test_342_base64_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def test_server_base64_encoding_issue():

# Create binary data that will definitely result in + and / characters
# 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
@server.read_resource()
Expand Down
4 changes: 2 additions & 2 deletions tests/server/fastmcp/prompts/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def fn(name: str, age: int = 30) -> str:
return f"Hello, {name}! You're {age} years old."

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

prompt = Prompt.from_function(fn)
with pytest.raises(ValueError):
await prompt.render(arguments=dict(age=40))
await prompt.render(arguments={"age": 40})

@pytest.mark.anyio
async def test_fn_returns_message(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/server/fastmcp/servers/test_file_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ async def test_read_resource_file(mcp: FastMCP):
@pytest.mark.anyio
async def test_delete_file(mcp: FastMCP, test_dir: Path):
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()


@pytest.mark.anyio
async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):
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_list = list(res_iter)
Expand Down