Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

[pre-commit.ci] auto fixes from pre-commit.com hooks

pre-commit-ci[bot]16849a0c32ec
diff --git a/sweagent/tools/commands.py b/sweagent/tools/commands.py
index b07a5c37..e4935c8d 100644
--- a/sweagent/tools/commands.py
+++ b/sweagent/tools/commands.py
@@ -185,7 +185,11 @@ class Command(BaseModel):
raise ValueError(msg)
if not arg.required:
found_optional = True
- duplicates = {name for name in {arg.name for arg in self.arguments} if [arg.name for arg in self.arguments].count(name) > 1}
+ duplicates = {
+ name
+ for name in {arg.name for arg in self.arguments}
+ if [arg.name for arg in self.arguments].count(name) > 1
+ }
if duplicates:
msg = f"Command '{self.name}': Duplicate argument names: {duplicates}"
raise ValueError(msg)
diff --git a/tests/test_tools_command_parsing.py b/tests/test_tools_command_parsing.py
index 5ae975b8..139cdaac 100644
--- a/tests/test_tools_command_parsing.py
+++ b/tests/test_tools_command_parsing.py
@@ -1,6 +1,7 @@
-from sweagent.tools.commands import Command, Argument
import pytest
+from sweagent.tools.commands import Argument, Command
+
def test_command_parsing_formats():
"""Test various signature formats and default parsing."""
@@ -11,7 +12,7 @@ def test_command_parsing_formats():
arguments=[
Argument(name="arg1", type="string", description="First argument", required=True),
Argument(name="arg2", type="integer", description="Second argument", required=False),
- ]
+ ],
)
assert command.invoke_format == "test_cmd {arg1} {arg2} "
@@ -20,12 +21,10 @@ def test_command_parsing_formats():
name="goto",
signature="goto <line_number>",
docstring="moves the window to show line_number",
- arguments=[
- Argument(name="line_number", type="integer", description="line number", required=True)
- ]
+ arguments=[Argument(name="line_number", type="integer", description="line number", required=True)],
)
assert command.invoke_format == "goto {line_number}"
-
+
# Optional brackets (stripped in invoke_format)
command = Command(
name="open",
@@ -33,11 +32,11 @@ def test_command_parsing_formats():
docstring="opens file at path",
arguments=[
Argument(name="path", type="string", description="file path", required=True),
- Argument(name="line_number", type="integer", description="line number", required=False)
- ]
+ Argument(name="line_number", type="integer", description="line number", required=False),
+ ],
)
assert command.invoke_format == 'open "{path}" {line_number}'
-
+
# Flag-style arguments
command = Command(
name="grep",
@@ -45,8 +44,8 @@ def test_command_parsing_formats():
docstring="search for pattern in file",
arguments=[
Argument(name="pattern", type="string", description="search pattern", required=True),
- Argument(name="file", type="string", description="file to search", required=True)
- ]
+ Argument(name="file", type="string", description="file to search", required=True),
+ ],
)
assert command.invoke_format == "grep --pattern {pattern} --file {file}"
@@ -64,10 +63,10 @@ def test_argument_validation():
docstring="bad argument order",
arguments=[
Argument(name="optional", type="string", description="optional", required=False),
- Argument(name="required", type="string", description="required", required=True)
- ]
+ Argument(name="required", type="string", description="required", required=True),
+ ],
)
-
+
# Duplicate argument names
with pytest.raises(ValueError, match="Duplicate argument names"):
Command(
@@ -75,36 +74,33 @@ def test_argument_validation():
docstring="duplicate args",
arguments=[
Argument(name="arg1", type="string", description="first", required=True),
- Argument(name="arg1", type="string", description="duplicate", required=True)
- ]
+ Argument(name="arg1", type="string", description="duplicate", required=True),
+ ],
)
def test_argument_name_patterns():
"""Test valid and invalid argument name patterns."""
# Valid names including single characters
- valid_names = [
- "a", "x", "n", "simple", "with_underscore", "with-dash",
- "with123numbers", "_starts_with_underscore"
- ]
-
+ valid_names = ["a", "x", "n", "simple", "with_underscore", "with-dash", "with123numbers", "_starts_with_underscore"]
+
for name in valid_names:
command = Command(
name="test",
docstring="test",
- arguments=[Argument(name=name, type="string", description="test", required=True)]
+ arguments=[Argument(name=name, type="string", description="test", required=True)],
)
assert command.arguments[0].name == name
# Invalid names
invalid_names = ["123starts_with_number", ""]
-
+
for name in invalid_names:
with pytest.raises(ValueError, match="Invalid argument name"):
Command(
name="test",
docstring="test",
- arguments=[Argument(name=name, type="string", description="test", required=True)]
+ arguments=[Argument(name=name, type="string", description="test", required=True)],
)
@@ -118,19 +114,17 @@ def test_signature_argument_consistency():
docstring="missing argument in signature",
arguments=[
Argument(name="existing_arg", type="string", description="exists", required=True),
- Argument(name="missing_arg", type="string", description="not in signature", required=True)
- ]
+ Argument(name="missing_arg", type="string", description="not in signature", required=True),
+ ],
)
-
+
# Extra argument in signature
with pytest.raises(ValueError, match="Argument names.*do not match"):
Command(
name="extra_arg",
signature="extra_arg <arg1> <extra>",
docstring="extra argument in signature",
- arguments=[
- Argument(name="arg1", type="string", description="exists", required=True)
- ]
+ arguments=[Argument(name="arg1", type="string", description="exists", required=True)],
)
@@ -141,22 +135,24 @@ def test_function_calling_tool_generation():
docstring="A test function for OpenAI",
arguments=[
Argument(name="required_arg", type="string", description="Required string argument", required=True),
- Argument(name="enum_arg", type="string", description="Enum argument", required=True, enum=["option1", "option2"]),
- Argument(name="optional_arg", type="integer", description="Optional integer argument", required=False)
- ]
+ Argument(
+ name="enum_arg", type="string", description="Enum argument", required=True, enum=["option1", "option2"]
+ ),
+ Argument(name="optional_arg", type="integer", description="Optional integer argument", required=False),
+ ],
)
-
+
tool = command.get_function_calling_tool()
-
+
assert tool["type"] == "function"
assert tool["function"]["name"] == "test_function"
assert tool["function"]["description"] == "A test function for OpenAI"
-
+
properties = tool["function"]["parameters"]["properties"]
assert properties["required_arg"]["type"] == "string"
assert properties["optional_arg"]["type"] == "integer"
assert properties["enum_arg"]["enum"] == ["option1", "option2"]
-
+
required = tool["function"]["parameters"]["required"]
assert "required_arg" in required
assert "enum_arg" in required
@@ -170,11 +166,9 @@ def test_multiline_command():
signature="edit <filename>",
docstring="Edit a file with multi-line content",
end_name="EOF",
- arguments=[
- Argument(name="filename", type="string", description="file to edit", required=True)
- ]
+ arguments=[Argument(name="filename", type="string", description="file to edit", required=True)],
)
-
+
assert command.invoke_format == "edit {filename}"
assert command.end_name == "EOF"
@@ -186,14 +180,14 @@ def test_custom_argument_format():
docstring="Test custom argument formatting",
arguments=[
Argument(
- name="arg1",
- type="string",
- description="Custom formatted argument",
+ name="arg1",
+ type="string",
+ description="Custom formatted argument",
required=True,
- argument_format="--{value}"
+ argument_format="--{value}",
)
- ]
+ ],
)
-
+
assert command.arguments[0].argument_format == "--{value}"
assert command.invoke_format == "custom_format {arg1} "