Skip to content

Commit

Permalink
Include underlying error when AST safety check parsing fails (#2693)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 committed Dec 15, 2021
1 parent 93701d2 commit 3501cef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/black/__init__.py
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_black.py
Expand Up @@ -1584,6 +1584,16 @@ def test_for_handled_unexpected_eof_error(self) -> None:

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"\(<unknown>, line 1\)")


class TestCaching:
def test_cache_broken_file(self) -> None:
Expand Down

0 comments on commit 3501cef

Please sign in to comment.