style: Run linter on patch_flex_coder.py

This commit is contained in:
Paul Gauthier (aider) 2025-04-14 15:29:14 -07:00
parent 5573cdfba1
commit 4e1e77890b

View file

@ -10,7 +10,6 @@ from ..dump import dump # noqa: F401
from .base_coder import Coder
from .patch_prompts import PatchPrompts
# Import search_replace utilities
from .search_replace import editblock_strategies, flexible_search_and_replace
@ -221,10 +220,7 @@ class PatchFlexCoder(Coder): # Rename class
lines = content.splitlines()
start_index = 0
if (
len(lines) >= 2
and _norm(lines[0]).startswith("*** Begin Patch")
):
if len(lines) >= 2 and _norm(lines[0]).startswith("*** Begin Patch"):
start_index = 1
else:
# Tolerate missing sentinels if content looks like a patch action
@ -237,9 +233,7 @@ class PatchFlexCoder(Coder): # Rename class
if not is_patch_like:
self.io.tool_warning("Response does not appear to be in patch format.")
return []
self.io.tool_warning(
"Patch format warning: Missing '*** Begin Patch' sentinel."
)
self.io.tool_warning("Patch format warning: Missing '*** Begin Patch' sentinel.")
# Identify files needed for context lookups (only for DELETE check)
needed_paths = identify_files_needed(content)
@ -248,7 +242,6 @@ class PatchFlexCoder(Coder): # Rename class
# We read content dynamically in apply_edits.
known_files = set(self.get_inchat_relative_files()) | set(needed_paths)
try:
# Parse the patch text into ParsedEdit objects
parsed_edits = self._parse_patch_text(lines, start_index, known_files)
@ -310,7 +303,9 @@ class PatchFlexCoder(Coder): # Rename class
# Check against known files before adding delete action
self.io.tool_warning(f"Delete File target '{path}' not found in chat context.")
parsed_edits.append(ParsedEdit(path=path, type=ActionType.DELETE, patch_line_num=line_num))
parsed_edits.append(
ParsedEdit(path=path, type=ActionType.DELETE, patch_line_num=line_num)
)
current_file_path = None # Reset current file context
current_move_path = None
continue
@ -353,7 +348,6 @@ class PatchFlexCoder(Coder): # Rename class
except DiffError as e:
raise DiffError(f"{e} (near line {line_num} in patch)")
if not del_lines and not ins_lines:
# Skip hunks that contain only context - they don't represent a change
index = next_index
@ -380,7 +374,6 @@ class PatchFlexCoder(Coder): # Rename class
search_text += "\n"
replace_text += "\n"
parsed_edits.append(
ParsedEdit(
path=current_file_path,
@ -399,7 +392,9 @@ class PatchFlexCoder(Coder): # Rename class
index += 1
continue
raise DiffError(f"Unknown or misplaced line while parsing patch (line {line_num}): {line}")
raise DiffError(
f"Unknown or misplaced line while parsing patch (line {line_num}): {line}"
)
return parsed_edits
@ -435,7 +430,7 @@ class PatchFlexCoder(Coder): # Rename class
path="", # Path set by caller
type=ActionType.ADD,
new_content="\n".join(added_lines),
patch_line_num=start_line_num
patch_line_num=start_line_num,
)
return action, index
@ -465,7 +460,9 @@ class PatchFlexCoder(Coder): # Rename class
if edit.type == ActionType.ADD:
if path_obj.exists():
# Allow overwrite on ADD? Or error? Let's warn and overwrite.
self.io.tool_warning(f"ADD Warning: File '{path}' already exists, overwriting.")
self.io.tool_warning(
f"ADD Warning: File '{path}' already exists, overwriting."
)
# raise DiffError(f"ADD Error: File already exists: {path}")
if edit.new_content is None:
raise DiffError(f"ADD change for {path} has no content")
@ -480,13 +477,17 @@ class PatchFlexCoder(Coder): # Rename class
elif edit.type == ActionType.DELETE:
self.io.tool_output(f"Deleting {path}")
if not path_obj.exists():
self.io.tool_warning(f"DELETE Warning: File not found, skipping: {path}")
self.io.tool_warning(
f"DELETE Warning: File not found, skipping: {path}"
)
else:
path_obj.unlink()
except (DiffError, FileNotFoundError, IOError, OSError) as e:
raise ValueError(f"Error applying action '{edit.type}' to {path}: {e}")
except Exception as e:
raise ValueError(f"Unexpected error applying action '{edit.type}' to {path}: {e}")
raise ValueError(
f"Unexpected error applying action '{edit.type}' to {path}: {e}"
)
continue # Move to the next file path
# --- Handle UPDATE actions sequentially ---
@ -500,13 +501,17 @@ class PatchFlexCoder(Coder): # Rename class
for i, edit in enumerate(path_edits):
if edit.type != ActionType.UPDATE:
raise DiffError(f"Unexpected action type '{edit.type}' mixed with UPDATE for {path}")
raise DiffError(
f"Unexpected action type '{edit.type}' mixed with UPDATE for {path}"
)
if edit.search_text is None or edit.replace_text is None:
raise DiffError(f"UPDATE action for {path} is missing search/replace text")
final_move_path = edit.move_path # Last move path specified wins
self.io.tool_output(f" Applying hunk {i+1} (from patch line {edit.patch_line_num})...")
self.io.tool_output(
f" Applying hunk {i+1} (from patch line {edit.patch_line_num})..."
)
texts = (edit.search_text, edit.replace_text, current_content)
new_content = flexible_search_and_replace(texts, editblock_strategies)
@ -515,11 +520,11 @@ class PatchFlexCoder(Coder): # Rename class
edit_failed = True
# Provide more context on failure
err_msg = (
f"Failed to apply update hunk {i+1} (from patch line {edit.patch_line_num})"
f" for file {path}. The search block may not have been found"
" or the change conflicted.\n"
f"Search block:\n```\n{edit.search_text}```\n"
f"Replace block:\n```\n{edit.replace_text}```"
f"Failed to apply update hunk {i+1} (from patch line"
f" {edit.patch_line_num}) for file {path}. The search block may not"
" have been found or the change conflicted.\nSearch"
f" block:\n```\n{edit.search_text}```\nReplace"
f" block:\n```\n{edit.replace_text}```"
)
# Raise immediately to stop processing this file
raise ValueError(err_msg)