def valid_ipython_code()

in project/nanoeval/nanoeval/solvers/computer_tasks/code_execution_interface.py [0:0]


def valid_ipython_code(code: str) -> bool:
    # Parse the code using ast to check for syntax errors.
    # NOTE: technically, might be unsound if the ACE environment
    # has a newer Python than the one we're using here. But atm
    # (4/3/24), this is not the case.
    tm = TransformerManager()  # type: ignore
    transformed_code = tm.transform_cell(code)
    try:
        ast.parse(transformed_code)
        return True
    except SyntaxError:
        return False
    except Exception as e:
        raise ValueError("Error parsing code") from e