diff --git a/blacken_docs.py b/blacken_docs.py index 86ffc8c..8992117 100644 --- a/blacken_docs.py +++ b/blacken_docs.py @@ -128,6 +128,8 @@ def _rst_match(match: Match[str]) -> str: return f'{match["before"]}{code.rstrip()}{trailing_ws}' def _rst_literal_blocks_match(match: Match[str]) -> str: + if not match['code'].strip(): + return match[0] min_indent = min(INDENT_RE.findall(match['code'])) trailing_ws_match = TRAILING_NL_RE.search(match['code']) assert trailing_ws_match diff --git a/tests/blacken_docs_test.py b/tests/blacken_docs_test.py index c17f3ae..5c9114d 100644 --- a/tests/blacken_docs_test.py +++ b/tests/blacken_docs_test.py @@ -234,6 +234,23 @@ def test_format_src_rst_literal_blocks_nested(): assert errors == [] +def test_format_src_rst_literal_blocks_empty(): + before = dedent( + ''' + Example:: + + .. warning:: + + There was no example. + ''', + ) + after, errors = blacken_docs.format_str( + before, BLACK_MODE, rst_literal_blocks=True, + ) + assert after == before + assert errors == [] + + def test_format_src_rst_sphinx_doctest(): before = ( '.. testsetup:: group1\n'