From 7cdca479588eb4902f688a87d614606610e65ab9 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 12 Sep 2024 15:01:19 -0700 Subject: [PATCH 1/3] Report Black version on internal error --- src/black/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 7822e3d1c36..c335ddb4d2c 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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: @@ -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 @@ -1556,7 +1563,7 @@ 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" + f"INTERNAL ERROR: {_black_info()} 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}" ) from None @@ -1584,7 +1591,7 @@ def assert_stable( diff(dst, newdst, "first pass", "second pass"), ) raise AssertionError( - "INTERNAL ERROR: Black produced different code on the second pass of the" + f"INTERNAL ERROR: {_black_info()} 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}" ) from None From a45301b94588caf2089640b0b52608348432e915 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 12 Sep 2024 15:02:43 -0700 Subject: [PATCH 2/3] change --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 583a0be5646..b04a12b586d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -48,6 +48,7 @@ - Added Python target version information on parse error (#4378) +- Add information about Black version to internal error messages (#4457) ### _Blackd_ From c16ad1377f3fc24a7bb663565b711f9fe09c30b9 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 12 Sep 2024 15:07:02 -0700 Subject: [PATCH 3/3] . --- src/black/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index c335ddb4d2c..4918bc67610 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1563,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( - f"INTERNAL ERROR: {_black_info()} 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 @@ -1591,9 +1591,9 @@ def assert_stable( diff(dst, newdst, "first pass", "second pass"), ) raise AssertionError( - f"INTERNAL ERROR: {_black_info()} 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