docformatter icon indicating copy to clipboard operation
docformatter copied to clipboard

Doc formatter reformats correct docstring

Open probicheaux opened this issue 4 years ago • 2 comments

with docformatter 1.4, command docformatter --wrap-summaries 100 --wrap-descriptions 100 the following docstring

"""hello yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeet -v
"""

gets transformed to

"""hello yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeet
-v."""

which is fine (correct), but then if I run docformatter again, the correct string incorrectly becomes

"""hello yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeet.

-v.
"""

probicheaux avatar Oct 01 '21 21:10 probicheaux

Proposing this patch, which supposes that parameter lists in the description usually should have a one line description preceding them

diff --git a/docformatter.py b/docformatter.py
index 07213e7..0927ce0 100755
--- a/docformatter.py
+++ b/docformatter.py
@@ -296,9 +296,10 @@ def split_summary_and_description(contents):
         if not split_lines[index].strip():
             # Empty line separation would indicate the rest is the description.
             found = True
-        elif is_probably_beginning_of_sentence(split_lines[index]):
+        if index + 1 < len(split_lines):
             # Symbol on second line probably is a description with a list.
-            found = True
+            if is_probably_beginning_of_sentence(split_lines[index+1]):
+                found = True

         if found:
             return ('\n'.join(split_lines[:index]).strip(),
diff --git a/test_docformatter.py b/test_docformatter.py
index 0908135..0537ce8 100755
--- a/test_docformatter.py
+++ b/test_docformatter.py
@@ -426,6 +426,13 @@ Hello.
             docstring,
             docformatter.format_docstring('    ', docstring))

+    def test_format_docstring_leave_multilines_summaries_with_symbols_alone(self):
+        docstring = '''"""hello yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeet
+    -v."""'''
+        self.assertEqual(
+            docstring,
+            docformatter.format_docstring('    ', docstring))
+
     def test_format_code(self):
         self.assertEqual(
             '''\
@@ -957,7 +964,7 @@ def foo():\r
                              'This is the first\nWashington'))

     def test_split_summary_and_description_with_list_on_other_line(self):
-        self.assertEqual(('Test\n    test', '    @blah'),
+        self.assertEqual(('Test', '    test\n    @blah'),
                          docformatter.split_summary_and_description('''\
     Test
     test

probicheaux avatar Oct 02 '21 23:10 probicheaux

@probicheaux could you open a pull request with your proposed solution?

weibullguy avatar Jul 24 '22 17:07 weibullguy