def test_should_raise_if_wrong_format(): """ Test should raise if invalid syntax """ with pytest.raises(MlVToolException) as e: get_ast('a:=3') assert isinstance(e.value.__cause__, SyntaxError)
def test_script_must_not_be_ast_equals(script_base, diff_script_content): """ Test ast tree are not equal if diff docstring or statements """ base_ast = get_ast(script_base) diff_ast = get_ast(diff_script_content) assert not is_ast_equal(base_ast, diff_ast)
def test_script_must_be_ast_equals(script_base, diff_script_content): """ Test ast tree are equal even if diff on blank lines, whitespaces and comments """ base_ast = get_ast(script_base) diff_ast = get_ast(diff_script_content) assert is_ast_equal(base_ast, diff_ast)
def compare(notebook_path: str, script_path: str, conf: MlVToolConf) -> bool: """ Compare the script obtained by notebook conversion using ipynb_to_python with the actual script. """ generated_script = get_converted_script(notebook_path, conf) generated_ast = get_ast(generated_script, name=notebook_path) script_ast = get_ast_from_file(script_path) return is_ast_equal(generated_ast, script_ast)
def extract_docstring(cell_content: str) -> str: """ Extract a docstring from a cell content """ logging.info('Extract docstring from cell content') logging.debug(f'Cell content {cell_content}') docstring = '' try: root = get_ast(cell_content) except SyntaxError as e: raise MlVToolException( f'Invalid python cell format: {cell_content}') from e for node in ast.walk(root): if isinstance(node, ast.Module): docstring = ast.get_docstring(node) break return docstring
def test_should_parse_ast(): """ Tests should parse ast from string content """ python_content = 'import os\nprint("hello")' assert get_ast(python_content)