Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,21 @@ def format_docstring(indentation, docstring,
ending=ending
)
else:
return wrap_summary('"""' + normalize_summary(contents) + '"""',
wrap_length=summary_wrap_length,
initial_indent=indentation,
subsequent_indent=indentation).strip()
summary_wrapped = wrap_summary(
'"""' + normalize_summary(contents),
wrap_length=summary_wrap_length,
initial_indent=indentation,
subsequent_indent=indentation)

summary_is_one_line = '\n' not in summary_wrapped
quotes_fit_on_line = len(indentation) + len(summary_wrapped) + 3 <= summary_wrap_length
if not summary_wrap_length or (summary_is_one_line and quotes_fit_on_line):
ending = '"""'
else:
ending = '\n' + indentation + '"""'
return '{summary}{ending}'.format(
summary=summary_wrapped,
ending=ending)


def reindent(text, indentation):
Expand Down
31 changes: 25 additions & 6 deletions test_docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,13 +1141,14 @@ def test_force_wrap(self):
self.assertEqual(('''\
"""num_iterations is the number of updates -
instead of a better definition of
convergence."""\
convergence.
"""\
'''),
docformatter.format_docstring(' ', '''\
"""
num_iterations is the number of updates - instead of a better definition of convergence.
"""\
''', description_wrap_length=50, summary_wrap_length=50, force_wrap=True))
''', description_wrap_length=49, summary_wrap_length=49, force_wrap=True))

def test_remove_section_header(self):
self.assertEqual(
Expand Down Expand Up @@ -1182,6 +1183,24 @@ def test_format_docstring_make_summary_multi_line(self):
"""This one-line docstring will be multi-line"""\
''', make_summary_multi_line=True))

def test_format_docstring_for_multi_line_summary_alone(self):
self.assertEqual(('''\
"""This one-line docstring will be multi-line because it's quite
long.
"""\
'''),
docformatter.format_docstring(' ', '''\
"""This one-line docstring will be multi-line because it's quite long."""\
''', summary_wrap_length=69))

def test_format_docstring_for_one_line_summary_alone_but_too_long(self):
self.assertEqual(('''\
"""This one-line docstring will not be multi-line but quotes will be below.
"""\
'''),
docformatter.format_docstring(' ', '''\
"""This one-line docstring will not be multi-line but quotes will be below."""\
''', summary_wrap_length=80))

class TestSystem(unittest.TestCase):

Expand Down Expand Up @@ -1282,13 +1301,13 @@ def foo():
process = run_docformatter(['--wrap-summaries=40',
filename])
self.assertEqual('''\
@@ -1,4 +1,3 @@
@@ -1,4 +1,4 @@
def foo():
- """
+ """Hello world this is a summary
+ that will get wrapped.
"""
- Hello world this is a summary that will get wrapped
- """
+ """Hello world this is a summary
+ that will get wrapped."""
''', '\n'.join(process.communicate()[0].decode().split('\n')[2:]))

def test_end_to_end_with_no_wrapping(self):
Expand Down