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]
Make tools compatible with python3.6
Kilian Lieret87e6e3af9e7f
diff --git a/tools/defaults/bin/goto b/tools/defaults/bin/gotoindex 2c1c7d3f..f2fb7537 100644--- a/tools/defaults/bin/goto+++ b/tools/defaults/bin/goto@@ -1,10 +1,11 @@#!/usr/bin/env python3import sys+from typing import Listfrom default_utils import WindowedFile # type: ignore-def main(args: list[str]) -> int:+def main(args: List[str]) -> int:if len(args) > 1:print("goto allows only one line number at a time.")return 1diff --git a/tools/defaults/bin/open b/tools/defaults/bin/openindex 8585242e..23593d1a 100644--- a/tools/defaults/bin/open+++ b/tools/defaults/bin/open@@ -1,6 +1,5 @@#!/usr/bin/env python3import sys-from pathlib import Pathfrom typing import Optionalfrom default_utils import FileNotOpened, WindowedFile # type: ignorediff --git a/tools/defaults/lib/default_utils.py b/tools/defaults/lib/default_utils.pyindex 494f958a..859de2fa 100644--- a/tools/defaults/lib/default_utils.py+++ b/tools/defaults/lib/default_utils.py@@ -1,14 +1,18 @@import jsonimport os-from collections.abc import Generator-from dataclasses import dataclassfrom pathlib import Path-from typing import Any, Optional, Union--from typing_extensions import Literal+from typing import Any, Optional, Union, List, Tupleclass EnvRegistry:+ """Read and write variables into a file. This is used to persist state between tool+ calls without using environment variables (which are problematic because you cannot+ set them in a subprocess).++ The default file location is `/root/.swe-agent-env`, though this can be overridden+ by the `env_file` argument or the `SWE_AGENT_ENV_FILE` environment variable.+ """+def __init__(self, env_file: Optional[Path] = None):self._env_file = env_file@@ -50,7 +54,7 @@ class TextNotFound(Exception):"""Raised when the text is not found in the window."""-def _find_all(a_str: str, sub: str) -> Generator[int, None, None]:+def _find_all(a_str: str, sub: str):start = 0while True:start = a_str.find(sub, start)@@ -60,28 +64,18 @@ def _find_all(a_str: str, sub: str) -> Generator[int, None, None]:start += len(sub)-@dataclassclass ReplacementInfo:- first_replaced_line: int- """Line number of the beginning of the first search hit."""-- n_search_lines: int- """Number of lines of search string"""+ def __init__(self, first_replaced_line: int, n_search_lines: int, n_replace_lines: int, n_replacements: int):+ self.first_replaced_line = first_replaced_line+ self.n_search_lines = n_search_lines+ self.n_replace_lines = n_replace_lines+ self.n_replacements = n_replacements- n_replace_lines: int- """Number of lines of replace string"""- n_replacements: int- """Number of replacements made."""---@dataclassclass InsertInfo:- first_inserted_line: int- """Line number of the beginning of the first search hit."""-- n_lines_added: int- """Number of lines added."""+ def __init__(self, first_inserted_line: int, n_lines_added: int):+ self.first_inserted_line = first_inserted_line+ self.n_lines_added = n_lines_addedclass WindowedFile:@@ -172,7 +166,7 @@ class WindowedFile:return len(self.text.splitlines())@property- def line_range(self) -> tuple[int, int]:+ def line_range(self) -> Tuple[int, int]:"""Return first and last line (inclusive) of the display window, suchthat exactly `window` many lines are displayed.This means `line_range[1] - line_range[0] == window-1` as long as there are@@ -193,7 +187,7 @@ class WindowedFile:if start_line > 0:out_lines.append(f"({start_line} more lines above)")if line_numbers:- out_lines.extend(f"{i+start_line+1}:{line}" for i, line in enumerate(lines))+ out_lines.extend(f"{i + start_line + 1}:{line}" for i, line in enumerate(lines))else:out_lines.extend(lines)if pre_post_line:@@ -202,7 +196,7 @@ class WindowedFile:return "\n".join(out_lines)def set_window_text(- self, new_text: str, *, line_range: Optional[tuple[int, int]] = None+ self, new_text: str, *, line_range: Optional[Tuple[int, int]] = None) -> None:"""Replace the text in the current display window with a new string.@@ -223,8 +217,8 @@ class WindowedFile:search: str,replace: str,*,- reset_first_line: Literal["top", "keep"] = "top",- ) -> ReplacementInfo:+ reset_first_line: str = "top",+ ) -> "ReplacementInfo":"""Search and replace in the window.Args:@@ -256,9 +250,7 @@ class WindowedFile:n_replacements=1,)- def replace(- self, search: str, replace: str, *, reset_first_line: Literal["top", "keep"] = "top"- ) -> ReplacementInfo:+ def replace(self, search: str, replace: str, *, reset_first_line: str = "top") -> "ReplacementInfo":indices = list(_find_all(self.text, search))if not indices:if self._exit_on_exception:@@ -282,7 +274,7 @@ class WindowedFile:def print_window(self, *, line_numbers: bool = True, status_line: bool = True, pre_post_line: bool = True):print(self.get_window_text(line_numbers=line_numbers, status_line=status_line, pre_post_line=pre_post_line))- def goto(self, line: int, mode: Literal["top"] = "top"):+ def goto(self, line: int, mode: str = "top"):if mode == "top":self.first_line = line - self.window * self.offset_multiplierelse:@@ -298,9 +290,7 @@ class WindowedFile:self.text = self._original_textself.first_line = self._original_first_line- def insert(- self, text: str, line: Optional[int] = None, *, reset_first_line: Literal["top", "keep"] = "top"- ) -> InsertInfo:+ def insert(self, text: str, line: Optional[int] = None, *, reset_first_line: str = "top") -> "InsertInfo":if line is None:if self.text.endswith("\n"):line = self.n_linesdiff --git a/tools/defaults/lib/flake8_utils.py b/tools/defaults/lib/flake8_utils.pyindex a67c721f..88adf162 100644--- a/tools/defaults/lib/flake8_utils.py+++ b/tools/defaults/lib/flake8_utils.py@@ -5,21 +5,20 @@# ruff: noqa: UP007 UP006 UP035import subprocess-from dataclasses import dataclassfrom pathlib import Pathfrom typing import List, Optional, Tuplefrom default_utils import registry-@dataclassclass Flake8Error:"""A class to represent a single flake8 error"""- filename: str- line_number: int- col_number: int- problem: str+ def __init__(self, filename: str, line_number: int, col_number: int, problem: str):+ self.filename = filename+ self.line_number = line_number+ self.col_number = col_number+ self.problem = problem@classmethoddef from_line(cls, line: str):@@ -31,6 +30,16 @@ class Flake8Error:raise ValueError(msg) from ereturn cls(filename, int(line_number), int(col_number), problem)+ def __eq__(self, other):+ if not isinstance(other, Flake8Error):+ return NotImplemented+ return (+ self.filename == other.filename+ and self.line_number == other.line_number+ and self.col_number == other.col_number+ and self.problem == other.problem+ )+def _update_previous_errors(previous_errors: List[Flake8Error], replacement_window: Tuple[int, int], replacement_n_lines: int