Skip to content

Commit

Permalink
fix: Never fail when trying to format code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 9, 2023
1 parent 47b4115 commit 2acf6f2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/mkdocstrings_handlers/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2acf6f2

Please sign in to comment.