Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASTVerifier #214

Merged
merged 6 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from darker.help import ISORT_INSTRUCTION
from darker.import_sorting import apply_isort, isort
from darker.linting import run_linters
from darker.utils import GIT_DATEFORMAT, TextDocument, get_common_root
from darker.verification import BinarySearch, NotEquivalentError, verify_ast_unchanged
from darker.utils import GIT_DATEFORMAT, TextDocument, debug_dump, get_common_root
from darker.verification import ASTVerifier, BinarySearch, NotEquivalentError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -143,6 +143,9 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals
max_context_lines = len(rev2_isorted.lines)
minimum_context_lines = BinarySearch(0, max_context_lines + 1)
last_successful_reformat = None

verifier = ASTVerifier(baseline=rev2_isorted)

while not minimum_context_lines.found:
context_lines = minimum_context_lines.get_next()
if context_lines > 0:
Expand Down Expand Up @@ -176,13 +179,8 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals
len(rev2_isorted.lines),
len(chosen.lines),
)
try:
verify_ast_unchanged(rev2_isorted, chosen, black_chunks, edited_linenums)
except NotEquivalentError:
# Diff produced misaligned chunks which couldn't be reconstructed into
# a partially re-formatted Python file which produces an identical AST.
# Try again with a larger `-U<context_lines>` option for `git diff`,
# or give up if `context_lines` is already very large.
if not verifier.is_equivalent_to_baseline(chosen):
debug_dump(black_chunks, edited_linenums)
logger.debug(
"AST verification of %s with %s lines of context failed",
src,
Expand Down
24 changes: 12 additions & 12 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from textwrap import dedent
from types import SimpleNamespace
from unittest.mock import Mock, patch
from unittest.mock import patch

import pytest
from black import find_project_root
Expand Down Expand Up @@ -44,12 +44,13 @@ def run_isort(git_repo, monkeypatch, caplog, request):
paths = git_repo.add({"test1.py": "original"}, commit="Initial commit")
paths["test1.py"].write_bytes(b"changed")
args = getattr(request, "param", ())
with patch.multiple(
darker.__main__,
run_black=Mock(return_value=TextDocument()),
verify_ast_unchanged=Mock(),
), patch("darker.import_sorting.isort_code") as isort_code:
isort_code.return_value = "dummy isort output"
isorted_code = "import os; import sys;"
blacken_code = "import os\nimport sys\n"
patch_run_black_ctx = patch.object(
darker.__main__, "run_black", return_value=TextDocument(blacken_code)
)
with patch_run_black_ctx, patch("darker.import_sorting.isort_code") as isort_code:
isort_code.return_value = isorted_code
darker.__main__.main(["--isort", "./test1.py", *args])
return SimpleNamespace(
isort_code=darker.import_sorting.isort_code, caplog=caplog
Expand Down Expand Up @@ -213,11 +214,10 @@ def test_format_edited_parts_ast_changed(git_repo, caplog):
caplog.set_level(logging.DEBUG, logger="darker.__main__")
paths = git_repo.add({"a.py": "1\n2\n3\n4\n5\n6\n7\n8\n"}, commit="Initial commit")
paths["a.py"].write_bytes(b"8\n7\n6\n5\n4\n3\n2\n1\n")
with patch.object(
darker.__main__, "verify_ast_unchanged"
) as verify_ast_unchanged, pytest.raises(NotEquivalentError):
verify_ast_unchanged.side_effect = NotEquivalentError

mock_ctx = patch.object(
darker.verification.ASTVerifier, "is_equivalent_to_baseline", return_value=False
)
with mock_ctx, pytest.raises(NotEquivalentError):
_ = list(
darker.__main__.format_edited_parts(
git_repo.root,
Expand Down
21 changes: 20 additions & 1 deletion src/darker/tests/test_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import pytest

from darker.utils import DiffChunk, TextDocument
from darker.verification import BinarySearch, NotEquivalentError, verify_ast_unchanged
from darker.verification import (
ASTVerifier,
BinarySearch,
NotEquivalentError,
verify_ast_unchanged,
)


@pytest.mark.kwparametrize(
Expand All @@ -28,6 +33,20 @@ def test_verify_ast_unchanged(dst_content, expect):
assert expect is None


def test_ast_verifier_is_equivalent():
"""``darker.verification.ASTVerifier.is_equivalent_to_baseline``"""
verifier = ASTVerifier(baseline=TextDocument.from_lines(["if True: pass"]))
assert verifier.is_equivalent_to_baseline(
TextDocument.from_lines(["if True:", " pass"])
)
assert not verifier.is_equivalent_to_baseline(
TextDocument.from_lines(["if False: pass"])
)
assert not verifier.is_equivalent_to_baseline(
TextDocument.from_lines(["if False:"])
)


def test_binary_search_premature_result():
"""``darker.verification.BinarySearch``"""
with pytest.raises(RuntimeError):
Expand Down
35 changes: 33 additions & 2 deletions src/darker/verification.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Verification for unchanged AST before and after reformatting"""

from typing import List
from typing import Dict, List

from black import assert_equivalent
from black import assert_equivalent, parse_ast, stringify_ast

from darker.utils import DiffChunk, TextDocument, debug_dump

Expand Down Expand Up @@ -67,3 +67,34 @@ def verify_ast_unchanged(
except AssertionError as exc_info:
debug_dump(black_chunks, edited_linenums)
raise NotEquivalentError(str(exc_info))


class ASTVerifier: # pylint: disable=too-few-public-methods
"""Verify if reformatted TextDocument is AST-equivalent to baseline

Keeps in-memory data about previous comparisons to improve performance.

"""

def __init__(self, baseline: TextDocument) -> None:
self._baseline_ast_str = self._to_ast_str(baseline)
self._comparisons: Dict[str, bool] = {baseline.string: True}

@staticmethod
def _to_ast_str(document: TextDocument) -> str:
return "\n".join(stringify_ast(parse_ast(document.string)))

def is_equivalent_to_baseline(self, document: TextDocument) -> bool:
"""Returns true if document is AST-equivalent to baseline"""
if document.string in self._comparisons:
return self._comparisons[document.string]

try:
document_ast_str = self._to_ast_str(document)
except SyntaxError:
comparison = False
else:
comparison = self._baseline_ast_str == document_ast_str

self._comparisons[document.string] = comparison
return self._comparisons[document.string]
akaihola marked this conversation as resolved.
Show resolved Hide resolved