From 3501cefb09eb8448bd82287840c9093f10c25299 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Tue, 14 Dec 2021 21:21:28 -0500 Subject: [PATCH 1/1] Include underlying error when AST safety check parsing fails (#2693) --- CHANGES.md | 2 ++ src/black/__init__.py | 2 +- tests/test_black.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index c73295c..9208be7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,8 @@ ### _Black_ - Improve error message for invalid regular expression (#2678) +- Improve error message when parsing fails during AST safety check by embedding the + underlying SyntaxError (#2693) - Fix mapping cases that contain as-expressions, like `case {"key": 1 | 2 as password}` (#2686) - No longer color diff headers white as it's unreadable in light themed terminals diff --git a/src/black/__init__.py b/src/black/__init__.py index 59018d0..f2efdec 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1305,7 +1305,7 @@ def assert_equivalent(src: str, dst: str, *, pass_num: int = 1) -> None: src_ast = parse_ast(src) except Exception as exc: raise AssertionError( - "cannot use --safe with this file; failed to parse source file." + f"cannot use --safe with this file; failed to parse source file: {exc}" ) from exc try: diff --git a/tests/test_black.py b/tests/test_black.py index 468f00f..63cd716 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1584,6 +1584,16 @@ class BlackTestCase(BlackBaseTestCase): exc_info.match("Cannot parse: 2:0: EOF in multi-line statement") + def test_equivalency_ast_parse_failure_includes_error(self) -> None: + with pytest.raises(AssertionError) as err: + black.assert_equivalent("a«»a = 1", "a«»a = 1") + + err.match("--safe") + # Unfortunately the SyntaxError message has changed in newer versions so we + # can't match it directly. + err.match("invalid character") + err.match(r"\(, line 1\)") + class TestCaching: def test_cache_broken_file(self) -> None: -- 2.39.5