Skip to content

Commit 1e9076a

Browse files
authored
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
1 parent 154028b commit 1e9076a

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/docformatter/strings.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
# SOFTWARE.
2424
"""This module provides docformatter string functions."""
2525

26+
27+
import contextlib
2628
# Standard Library Imports
2729
import re
2830

@@ -110,7 +112,13 @@ def normalize_summary(summary: str) -> str:
110112
and (not summary.startswith("#"))
111113
):
112114
summary += "."
113-
summary = summary[0].upper() + summary[1:]
115+
116+
with contextlib.suppress(IndexError):
117+
# Look for underscores in the first word, this would typically
118+
# indicate the first word is a variable name or some other
119+
# non-standard English word.
120+
if "_" not in summary.split(" ", 1)[0]:
121+
summary = summary[0].upper() + summary[1:]
114122

115123
return summary
116124

tests/test_string_functions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,29 @@ def test_normalize_summary_capitalize_first_letter(self):
206206
"don't lower case I'm"
207207
)
208208

209+
@pytest.mark.unit
210+
def test_normalize_summary_capitalize_first_letter_with_period(self):
211+
"""Capitalize the first letter of the summary even when ends in period.
212+
213+
See issue #184. See requirement docformatter_4.5.1.
214+
"""
215+
assert (
216+
"This is a summary that needs to be capped."
217+
== docformatter.normalize_summary(
218+
"this is a summary that needs to be capped."
219+
)
220+
)
221+
222+
@pytest.mark.unit
223+
def test_normalize_summary_dont_capitalize_first_letter_if_variable(self):
224+
"""Capitalize the first word unless it looks like a variable."""
225+
assert (
226+
"num_iterations should not be capitalized in this summary."
227+
== docformatter.normalize_summary(
228+
"num_iterations should not be capitalized in this summary"
229+
)
230+
)
231+
209232

210233
class TestSplitters:
211234
"""Class for testing the string splitting function.

0 commit comments

Comments
 (0)