From df24bbc640886e1da2d00a3b58c1aa7736cb1eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sat, 9 Sep 2023 19:24:08 +0200 Subject: [PATCH] fix: Never fail when trying to format code with Black --- src/mkdocstrings_handlers/python/rendering.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mkdocstrings_handlers/python/rendering.py b/src/mkdocstrings_handlers/python/rendering.py index 46b34c01..ef4b5071 100644 --- a/src/mkdocstrings_handlers/python/rendering.py +++ b/src/mkdocstrings_handlers/python/rendering.py @@ -351,14 +351,17 @@ def do_filter_objects( @lru_cache(maxsize=1) def _get_black_formatter() -> Callable[[str, int], str]: try: - from black import Mode, format_str + from black import InvalidInput, Mode, format_str except ModuleNotFoundError: logger.info("Formatting signatures requires Black to be installed.") return lambda text, _: text def formatter(code: str, line_length: int) -> str: mode = Mode(line_length=line_length) - return format_str(code, mode=mode) + try: + return format_str(code, mode=mode) + except InvalidInput: + return code return formatter