Skip to content

Commit

Permalink
Handle async
Browse files Browse the repository at this point in the history
  • Loading branch information
norhh committed Feb 21, 2024
1 parent 31ef831 commit a3ea56c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions BugsInPy/run_custom_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
from git import Repo, NoSuchPathError
from pathlib import Path
import subprocess
from typing import Dict
from typing import Dict, Union

from BugsInPy.utils import checkout

from BugsInPy.bgp import BGPConfig, run_test, InvalidExecutionOrderError


class ReplaceFunctionNode(ast.NodeTransformer):
def __init__(self, target_lineno: int, replacement_node: ast.FunctionDef):
def __init__(
self,
target_lineno: int,
replacement_node: Union[ast.FunctionDef, ast.AsyncFunctionDef],
):
self.target_lineno = target_lineno
self.replacement_node = replacement_node

def visit_FunctionDef(self, node: ast.FunctionDef):
def visit_FunctionDef(self, node: Union[ast.FunctionDef, ast.AsyncFunctionDef]):
# Check if the function has decorators
start_lineno = node.lineno
if node.decorator_list:
Expand All @@ -44,7 +48,9 @@ def replace_code(bug_data: Dict, repo_bug_id: str, file_path: Path) -> None:

# Parse the replacement code to get its AST
replacement_tree = ast.parse(replacement_code)
if not isinstance(replacement_tree.body[0], ast.FunctionDef):
if not isinstance(
replacement_tree.body[0], (ast.FunctionDef, ast.AsyncFunctionDef)
):
raise ValueError("Replacement code does not contain a function definition.")

# Use the NodeTransformer to replace the original function with the replacement
Expand Down

0 comments on commit a3ea56c

Please sign in to comment.