From 1e9076a07b42cfd6f017df8a7a5746113d03cd0b Mon Sep 17 00:00:00 2001 From: Doyle Rowland Date: Mon, 24 Apr 2023 15:37:20 -0400 Subject: [PATCH] fix: not capitalizing first word when summary ends in period (#185) * fix: not capitalizing first word in summary with period * test: for capitalizing first word in summary with period --- src/docformatter/strings.py | 10 +++++++++- tests/test_string_functions.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/docformatter/strings.py b/src/docformatter/strings.py index 0e73298..c42d2ee 100644 --- a/src/docformatter/strings.py +++ b/src/docformatter/strings.py @@ -23,6 +23,8 @@ # SOFTWARE. """This module provides docformatter string functions.""" + +import contextlib # Standard Library Imports import re @@ -110,7 +112,13 @@ def normalize_summary(summary: str) -> str: and (not summary.startswith("#")) ): summary += "." - summary = summary[0].upper() + summary[1:] + + with contextlib.suppress(IndexError): + # Look for underscores in the first word, this would typically + # indicate the first word is a variable name or some other + # non-standard English word. + if "_" not in summary.split(" ", 1)[0]: + summary = summary[0].upper() + summary[1:] return summary diff --git a/tests/test_string_functions.py b/tests/test_string_functions.py index ece741e..d1a1b2e 100644 --- a/tests/test_string_functions.py +++ b/tests/test_string_functions.py @@ -206,6 +206,29 @@ def test_normalize_summary_capitalize_first_letter(self): "don't lower case I'm" ) + @pytest.mark.unit + def test_normalize_summary_capitalize_first_letter_with_period(self): + """Capitalize the first letter of the summary even when ends in period. + + See issue #184. See requirement docformatter_4.5.1. + """ + assert ( + "This is a summary that needs to be capped." + == docformatter.normalize_summary( + "this is a summary that needs to be capped." + ) + ) + + @pytest.mark.unit + def test_normalize_summary_dont_capitalize_first_letter_if_variable(self): + """Capitalize the first word unless it looks like a variable.""" + assert ( + "num_iterations should not be capitalized in this summary." + == docformatter.normalize_summary( + "num_iterations should not be capitalized in this summary" + ) + ) + class TestSplitters: """Class for testing the string splitting function.