From 13a3d2dff57b535e514055964a06c5c2af3c6db2 Mon Sep 17 00:00:00 2001 From: RavSinghChandan Date: Mon, 30 Nov 2026 15:54:46 +0530 Subject: [PATCH 1/2] fix: split the summary on the first run when the second line starts a new sentence --- src/docformatter/strings.py | 18 ++++++++++++++++++ tests/_data/string_files/do_format_code.toml | 18 ++++++++++++++++++ tests/formatter/test_do_format_code.py | 1 + 3 files changed, 37 insertions(+) diff --git a/src/docformatter/strings.py b/src/docformatter/strings.py index c6d1fb0..a67e489 100644 --- a/src/docformatter/strings.py +++ b/src/docformatter/strings.py @@ -455,10 +455,28 @@ def do_split_summary(lines) -> List[str]: f"{_leading_spaces}{rest_text}{_internal_spaces}" f"{lines[_pos + 1].strip()}" ) + elif _ends_summary_before_new_sentence(lines): + lines.insert(1, "") return lines +def _ends_summary_before_new_sentence(lines) -> bool: + """Check if the first line is a sentence and the second one starts another. + + Only matters when a blank line follows later, since the summary would + otherwise run up to that blank line. + """ + return ( + len(lines) > 2 # noqa: PLR2004 + and any(not line.strip() for line in lines[2:]) + and lines[0].endswith(".") + and not lines[0].endswith(ABBREVIATIONS) + and lines[0].count("``") % 2 == 0 + and lines[1].lstrip()[:1].isupper() + ) + + def do_split_summary_and_description(contents): """Split docstring into summary and description. diff --git a/tests/_data/string_files/do_format_code.toml b/tests/_data/string_files/do_format_code.toml index 17b93a6..c2c17ba 100644 --- a/tests/_data/string_files/do_format_code.toml +++ b/tests/_data/string_files/do_format_code.toml @@ -1348,3 +1348,21 @@ expected='''def foo(): a """) ''' +[issue_315] +source='''def length_hint(obj, default=0): + """ + Return an estimate of the number of items in obj. + This is useful for presizing containers when building from an iterable. + + If the object supports len(), the result will be exact. + """ +''' +expected='''def length_hint(obj, default=0): + """Return an estimate of the number of items in obj. + + This is useful for presizing containers when building from an + iterable. + + If the object supports len(), the result will be exact. + """ +''' diff --git a/tests/formatter/test_do_format_code.py b/tests/formatter/test_do_format_code.py index ef84584..a0d4c6d 100644 --- a/tests/formatter/test_do_format_code.py +++ b/tests/formatter/test_do_format_code.py @@ -152,6 +152,7 @@ ("issue_366_triple_quote_in_parens_not_docstring", NO_ARGS), ("issue_343_return_raw_string", NO_ARGS), ("issue_343_call_argument", NO_ARGS), + ("issue_315", NO_ARGS), ], ) def test_do_format_code(test_key, test_args, args): From 16cd54f6708443e665d69ecfc23dff1d8538d5c0 Mon Sep 17 00:00:00 2001 From: RavSinghChandan Date: Thu, 3 Dec 2026 08:46:28 +0530 Subject: [PATCH 2/2] style: wrap the new docstring as docformatter formats it --- src/docformatter/strings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/docformatter/strings.py b/src/docformatter/strings.py index a67e489..5f799ec 100644 --- a/src/docformatter/strings.py +++ b/src/docformatter/strings.py @@ -464,8 +464,8 @@ def do_split_summary(lines) -> List[str]: def _ends_summary_before_new_sentence(lines) -> bool: """Check if the first line is a sentence and the second one starts another. - Only matters when a blank line follows later, since the summary would - otherwise run up to that blank line. + Only matters when a blank line follows later, since the summary would otherwise run + up to that blank line. """ return ( len(lines) > 2 # noqa: PLR2004