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

Include underlying error when AST safety check parsing fails #2693

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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