project-a18hs5/helpers/generic_helpers.py

25 lines
1.1 KiB
Python

from typing import Optional
def fix_invalid_syntax_error(error_message: str) -> Optional[str]:
"""
Attempts to fix an invalid syntax error by removing the offending line.
Args:
error_message (str): The error message containing the invalid syntax error.
Returns:
Optional[str]: The modified code with the offending line removed, or None if the error message
does not contain a valid line number and code snippet.
"""
# Check if the error message follows the expected format
if "Code snippet near error:" in error_message:
lines = error_message.split("\n")
for i, line in enumerate(lines):
if line.startswith("Code snippet near error:"):
code_line_number = int(lines[i - 1].split(":")[0])
code_snippet = "\n".join(lines[i + 1:])
code_lines = code_snippet.split("\n")
if code_line_number <= len(code_lines):
modified_code = "\n".join(code_lines[:code_line_number - 1] + code_lines[code_line_number:])
return modified_code
return None