Skip to content

Commit

Permalink
lint and update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
CTY-git committed Jan 21, 2025
1 parent aedaac2 commit b87eb7e
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 354 deletions.
4 changes: 2 additions & 2 deletions patchwork/common/utils/input_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json
from collections.abc import Iterable, Mapping
from shutil import posix

from typing_extensions import AnyStr, Union

Expand Down Expand Up @@ -72,7 +71,8 @@ def parse_to_list(
rv.append(stripped_value)
return rv

def parse_to_dict(possible_dict, limit = -1):

def parse_to_dict(possible_dict, limit=-1):
if possible_dict is None and limit == 0:
return None

Expand Down
16 changes: 7 additions & 9 deletions patchwork/steps/FixIssue/FixIssue.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import difflib
import re
from pathlib import Path
from typing import Any, Optional

from git import Repo, InvalidGitRepositoryError
from patchwork.logger import logger
from git import InvalidGitRepositoryError, Repo
from openai.types.chat import ChatCompletionMessageParam

from patchwork.common.client.llm.aio import AioLlmClient
Expand All @@ -15,6 +13,7 @@
AnalyzeImplementStrategy,
)
from patchwork.common.tools import CodeEditTool, Tool
from patchwork.logger import logger
from patchwork.step import Step
from patchwork.steps.FixIssue.typed import FixIssueInputs, FixIssueOutputs

Expand Down Expand Up @@ -100,7 +99,7 @@ def is_stop(self, messages: list[ChatCompletionMessageParam]) -> bool:
class FixIssue(Step, input_class=FixIssueInputs, output_class=FixIssueOutputs):
def __init__(self, inputs):
"""Initialize the FixIssue step.
Args:
inputs: Dictionary containing input parameters including:
- base_path: Optional path to the repository root
Expand Down Expand Up @@ -145,12 +144,12 @@ def __init__(self, inputs):

def run(self):
"""Execute the FixIssue step.
This method:
1. Executes the multi-turn LLM conversation to analyze and fix the issue
2. Tracks file modifications made by the CodeEditTool
3. Generates in-memory diffs for all modified files
Returns:
dict: Dictionary containing list of modified files with their diffs
"""
Expand All @@ -162,8 +161,7 @@ def run(self):
if not isinstance(tool, CodeEditTool):
continue
tool_modified_files = [
dict(path=str(file_path.relative_to(cwd)), diff="")
for file_path in tool.tool_records["modified_files"]
dict(path=str(file_path.relative_to(cwd)), diff="") for file_path in tool.tool_records["modified_files"]
]
modified_files.extend(tool_modified_files)

Expand All @@ -174,7 +172,7 @@ def run(self):
file = modified_file["path"]
try:
# Try to get the diff using git
diff = self.repo.git.diff('HEAD', file)
diff = self.repo.git.diff("HEAD", file)
modified_file["diff"] = diff or ""
except Exception as e:
# Git-specific errors (untracked files, etc) - keep empty diff
Expand Down
8 changes: 5 additions & 3 deletions patchwork/steps/FixIssue/typed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing_extensions import Annotated, Dict, List, TypedDict
from typing_extensions import Annotated, List, TypedDict

from patchwork.common.constants import TOKEN_URL
from patchwork.common.utils.step_typing import StepTypeConfig
Expand Down Expand Up @@ -37,19 +37,21 @@ class FixIssueInputs(__FixIssueRequiredInputs, total=False):

class ModifiedFile(TypedDict):
"""Represents a file that has been modified by the FixIssue step.
Attributes:
path: The relative path to the modified file from the repository root
diff: A unified diff string showing the changes made to the file.
Generated using Python's difflib to compare the original and
modified file contents in memory.
Note:
The diff is generated by comparing file contents before and after
modifications, without relying on version control systems.
"""

path: str
diff: str


class FixIssueOutputs(TypedDict):
modified_files: List[ModifiedFile]
34 changes: 16 additions & 18 deletions patchwork/steps/ModifyCode/ModifyCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def save_file_contents(file_path: str | Path, content: str) -> None:
"""Utility function to save content to a file.
Args:
file_path: Path to the file to save content to (str or Path)
content: Content to write to the file
Expand Down Expand Up @@ -47,7 +47,7 @@ def replace_code_in_file(
new_code: str,
) -> None:
"""Replace code in a file at the specified line range.
Args:
file_path: Path to the file to modify (str or Path)
start_line: Starting line number (1-based)
Expand Down Expand Up @@ -109,38 +109,36 @@ def run(self) -> dict:
try:
# Store original content in memory
original_content = file_path.read_text() if file_path.exists() else ""

# Apply the changes
replace_code_in_file(file_path, start_line, end_line, new_code)

# Read modified content
current_content = file_path.read_text() if file_path.exists() else ""

# Generate unified diff
fromfile = f"a/{file_path}"
tofile = f"b/{file_path}"
diff = "".join(difflib.unified_diff(
original_content.splitlines(keepends=True),
current_content.splitlines(keepends=True),
fromfile=fromfile,
tofile=tofile
))

diff = "".join(
difflib.unified_diff(
original_content.splitlines(keepends=True),
current_content.splitlines(keepends=True),
fromfile=fromfile,
tofile=tofile,
)
)

if not diff and new_code: # If no diff but we have new code (new file)
diff = f"+++ {file_path}\n{new_code}"
except (OSError, IOError) as e:
logger.warning(f"Failed to generate diff for {file_path}: {str(e)}")
# Still proceed with the modification even if diff generation fails
replace_code_in_file(file_path, start_line, end_line, new_code)
diff = f"+++ {file_path}\n{new_code}" # Use new code as diff on error

# Create the modified code file dictionary
modified_code_file = dict(
path=str(file_path),
start_line=start_line,
end_line=end_line,
diff=diff,
**extracted_response
path=str(file_path), start_line=start_line, end_line=end_line, diff=diff, **extracted_response
)
modified_code_files.append(modified_code_file)

Expand Down
5 changes: 3 additions & 2 deletions patchwork/steps/ModifyCode/typed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ class ModifyCodeOutputs(TypedDict):

class ModifiedCodeFile(TypedDict, total=False):
"""Represents a file that has been modified by the ModifyCode step.
Attributes:
path: The path to the modified file
start_line: The starting line number of the modification (1-based)
end_line: The ending line number of the modification (1-based)
diff: A unified diff string showing the changes made to the file.
Generated using Python's difflib for in-memory comparison
of original and modified file contents.
Note:
The diff field is generated using difflib.unified_diff() to compare
the original and modified file contents in memory, ensuring efficient
and secure diff generation.
"""

path: str
start_line: int
end_line: int
Expand Down
Loading

0 comments on commit b87eb7e

Please sign in to comment.