Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Add ruff for shared mutable defaults (B) (#1938)

Engel Nystb9a5be256947
diff --git a/agenthub/SWE_agent/prompts.py b/agenthub/SWE_agent/prompts.py
index 9022367836..95a39b6f08 100644
--- a/agenthub/SWE_agent/prompts.py
+++ b/agenthub/SWE_agent/prompts.py
@@ -171,8 +171,9 @@ Begin with your thought about the next step and then come up with an action to p
""".strip()
-def unpack_dict(data: dict, restrict: list[str] = []):
+def unpack_dict(data: dict, restrict: list[str] | None = None):
lines = []
+ restrict = [] if restrict is None else restrict
for key, value in data.items():
if key in restrict:
continue
diff --git a/agenthub/micro/registry.py b/agenthub/micro/registry.py
index 2fc4060dc4..ecc65e4684 100644
--- a/agenthub/micro/registry.py
+++ b/agenthub/micro/registry.py
@@ -13,8 +13,7 @@ for dir in os.listdir(os.path.dirname(__file__)):
promptFile = base + '/prompt.md'
agentFile = base + '/agent.yaml'
if not os.path.isfile(promptFile) or not os.path.isfile(agentFile):
- raise Exception(
- f'Missing prompt or agent file in {base}. Please create them.')
+ raise Exception(f'Missing prompt or agent file in {base}. Please create them.')
with open(promptFile, 'r') as f:
prompt = f.read()
with open(agentFile, 'r') as f:
diff --git a/agenthub/monologue_agent/utils/prompts.py b/agenthub/monologue_agent/utils/prompts.py
index d1b37a062d..cab27808a8 100644
--- a/agenthub/monologue_agent/utils/prompts.py
+++ b/agenthub/monologue_agent/utils/prompts.py
@@ -108,7 +108,7 @@ def get_summarize_monologue_prompt(thoughts: list[dict]):
def get_request_action_prompt(
task: str,
thoughts: list[dict],
- background_commands_obs: list[CmdOutputObservation] = [],
+ background_commands_obs: list[CmdOutputObservation] | None = None,
):
"""
Gets the action prompt formatted with appropriate values.
@@ -122,6 +122,9 @@ def get_request_action_prompt(
- str: Formatted prompt string with hint, task, monologue, and background included
"""
+ if background_commands_obs is None:
+ background_commands_obs = []
+
hint = ''
if len(thoughts) > 0:
latest_thought = thoughts[-1]
diff --git a/dev_config/python/ruff.toml b/dev_config/python/ruff.toml
index 170034c68c..498e597046 100644
--- a/dev_config/python/ruff.toml
+++ b/dev_config/python/ruff.toml
@@ -9,10 +9,17 @@ select = [
"F",
"I",
"Q",
+ "B",
]
ignore = [
"E501",
+ "B003",
+ "B007",
+ "B009",
+ "B010",
+ "B904",
+ "B018",
]
[lint.flake8-quotes]
diff --git a/opendevin/controller/state/task.py b/opendevin/controller/state/task.py
index a47a2a01da..03c54c4d16 100644
--- a/opendevin/controller/state/task.py
+++ b/opendevin/controller/state/task.py
@@ -29,7 +29,7 @@ class Task:
parent: 'Task',
goal: str,
state: str = OPEN_STATE,
- subtasks: list = [],
+ subtasks: list = [], # noqa: B006
):
"""Initializes a new instance of the Task class.
@@ -45,6 +45,7 @@ class Task:
self.id = str(len(parent.subtasks))
self.parent = parent
self.goal = goal
+ logger.debug('Creating task {self.id} with parent={parent.id}, goal={goal}')
self.subtasks = []
for subtask in subtasks or []:
if isinstance(subtask, Task):
@@ -53,6 +54,7 @@ class Task:
goal = subtask.get('goal')
state = subtask.get('state')
subtasks = subtask.get('subtasks')
+ logger.debug('Reading: {goal}, {state}, {subtasks}')
self.subtasks.append(Task(self, goal, state, subtasks))
self.state = OPEN_STATE
@@ -190,7 +192,7 @@ class RootTask(Task):
task = task.subtasks[part]
return task
- def add_subtask(self, parent_id: str, goal: str, subtasks: list = []):
+ def add_subtask(self, parent_id: str, goal: str, subtasks: list | None = None):
"""Adds a subtask to a parent task.
Args:
@@ -198,6 +200,7 @@ class RootTask(Task):
goal: The goal of the subtask.
subtasks: A list of subtasks associated with the new subtask.
"""
+ subtasks = subtasks or []
parent = self.get_task_by_id(parent_id)
child = Task(parent=parent, goal=goal, subtasks=subtasks)
parent.subtasks.append(child)
@@ -210,6 +213,7 @@ class RootTask(Task):
state: The new state of the subtask.
"""
task = self.get_task_by_id(id)
+ logger.debug('Setting task {task.id} from state {task.state} to {state}')
task.set_state(state)
unfinished_tasks = [
t
diff --git a/opendevin/core/logger.py b/opendevin/core/logger.py
index 5d0b1f6fbf..a2ade32e37 100644
--- a/opendevin/core/logger.py
+++ b/opendevin/core/logger.py
@@ -114,10 +114,11 @@ def get_console_handler():
return console_handler
-def get_file_handler(log_dir=os.path.join(os.getcwd(), 'logs')):
+def get_file_handler(log_dir=None):
"""
Returns a file handler for logging.
"""
+ log_dir = os.path.join(os.getcwd(), 'logs') if log_dir is None else log_dir
os.makedirs(log_dir, exist_ok=True)
timestamp = datetime.now().strftime('%Y-%m-%d')
file_name = f'opendevin_{timestamp}.log'