Skip to content

Commit

Permalink
Report Black version on internal error (#4457)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Sep 12, 2024
1 parent 98a580b commit 058da5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<!-- Changes to Black's terminal output and error messages -->

- Added Python target version information on parse error (#4378)
- Add information about Black version to internal error messages (#4457)

### _Blackd_

Expand Down
21 changes: 14 additions & 7 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,13 @@ def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
return imports


def _black_info() -> str:
return (
f"Black {__version__} on "
f"Python ({platform.python_implementation()}) {platform.python_version()}"
)


def assert_equivalent(src: str, dst: str) -> None:
"""Raise AssertionError if `src` and `dst` aren't equivalent."""
try:
Expand All @@ -1546,7 +1553,7 @@ def assert_equivalent(src: str, dst: str) -> None:
except Exception as exc:
log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
raise ASTSafetyError(
f"INTERNAL ERROR: Black produced invalid code: {exc}. "
f"INTERNAL ERROR: {_black_info()} produced invalid code: {exc}. "
"Please report a bug on https://github.com/psf/black/issues. "
f"This invalid output might be helpful: {log}"
) from None
Expand All @@ -1556,9 +1563,9 @@ def assert_equivalent(src: str, dst: str) -> None:
if src_ast_str != dst_ast_str:
log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
raise ASTSafetyError(
"INTERNAL ERROR: Black produced code that is not equivalent to the"
" source. Please report a bug on "
f"https://github.com/psf/black/issues. This diff might be helpful: {log}"
f"INTERNAL ERROR: {_black_info()} produced code that is not equivalent to"
" the source. Please report a bug on https://github.com/psf/black/issues."
f" This diff might be helpful: {log}"
) from None


Expand All @@ -1584,9 +1591,9 @@ def assert_stable(
diff(dst, newdst, "first pass", "second pass"),
)
raise AssertionError(
"INTERNAL ERROR: Black produced different code on the second pass of the"
" formatter. Please report a bug on https://github.com/psf/black/issues."
f" This diff might be helpful: {log}"
f"INTERNAL ERROR: {_black_info()} produced different code on the second"
" pass of the formatter. Please report a bug on"
f" https://github.com/psf/black/issues. This diff might be helpful: {log}"
) from None


Expand Down

0 comments on commit 058da5f

Please sign in to comment.