def assert_code_snippet_executes_correctly(
    snippet: str, state: dict, line_number: int | None = None
):
    """Executes a snippet and compares output / errors to annotations."""

    raises_annotation = re.search(r"# raises\s*(\S*)", snippet)
    if raises_annotation is None:
        before = snippet
        after = None
        expected_failure = None
    else:
        before = snippet[: raises_annotation.start()]
        after = snippet[raises_annotation.start() :]
        expected_failure = raises_annotation.group(1)
        if not expected_failure:
            raise AssertionError('No error type specified for # raises line.')

    assert_code_snippet_runs_and_prints_expected(before, state, line_number)
    if expected_failure is not None:
        assert after is not None
        assert_code_snippet_fails(after, state, expected_failure)
Executes a snippet and compares output / errors to annotations.
dev_tools/snippets_test.py