diff --git a/CHANGES.rst b/CHANGES.rst index 0ab6c49c0..a61908215 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,8 +8,10 @@ Added Fixed ----- -- Black 24.2 compatibility by using the new `darkgraylib.files.find_project_root` +- Black 24.2.0 compatibility by using the new `darkgraylib.files.find_project_root` instead of the implementation in Black. +- Black 24.2.1 compatibility by detecting the new `black.parsing.ASTSafetyError` instead + of `AssertionError` when Black>=24.2.1 is in use. 1.7.3_ - 2024-02-27 diff --git a/src/darker/verification.py b/src/darker/verification.py index 1a89bac3b..da385857f 100644 --- a/src/darker/verification.py +++ b/src/darker/verification.py @@ -7,6 +7,13 @@ from darker.utils import debug_dump from darkgraylib.utils import DiffChunk, TextDocument +try: + # Black 24.2.1 and later + from black.parsing import ASTSafetyError # pylint: disable=ungrouped-imports +except ImportError: + # Black 24.2.0 and earlier + ASTSafetyError = AssertionError + class NotEquivalentError(Exception): """Exception to raise if two ASTs being compared are not equivalent""" @@ -65,7 +72,7 @@ def verify_ast_unchanged( """Verify that source code parses to the same AST before and after reformat""" try: assert_equivalent(edited_to_file.string, reformatted.string) - except AssertionError as exc_info: + except ASTSafetyError as exc_info: debug_dump(black_chunks, edited_linenums) raise NotEquivalentError() from exc_info