Skip to content

Commit

Permalink
fix: remove blank line after method docstring (#138)
Browse files Browse the repository at this point in the history
* feat: remove blank lines after method docstring

* test: add test for remove blank lines after method docstring

* chore: fix do release workflow
  • Loading branch information
weibullguy authored Dec 18, 2022
1 parent f7d50ae commit 5bae271
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/do-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ jobs:
- name: Get new version
id: newversion
run: |
pip install poetry
echo "::set-output name=version::$(echo $(poetry version | cut -d' ' -f2))"
pip install poetry
echo "::set-output name=version::$(echo $(poetry version | cut -d' ' -f2))"
- name: Get next semantic version
id: nextversion
Expand Down
31 changes: 29 additions & 2 deletions src/docformatter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
# SOFTWARE.
"""This module provides docformatter's Formattor class."""


# Standard Library Imports
import argparse
import collections
import contextlib
import io
import tokenize
from typing import TextIO, Tuple
Expand All @@ -34,11 +36,10 @@
import untokenize

# docformatter Package Imports
import docformatter.encode as _encode
import docformatter.strings as _strings
import docformatter.syntax as _syntax
import docformatter.util as _util
import docformatter.encode as _encode


unicode = str

Expand Down Expand Up @@ -327,6 +328,9 @@ def _format_code(
modified_tokens.append(
(token_type, token_string, start, end, line)
)
modified_tokens = self._do_remove_blank_lines_after_method(
modified_tokens
)

return untokenize.untokenize(modified_tokens)
except tokenize.TokenError:
Expand Down Expand Up @@ -446,6 +450,29 @@ def _do_format_docstring(
).strip()
return f"{beginning}{summary_wrapped}{ending}"

def _do_remove_blank_lines_after_method(self, modified_tokens):
"""Remove blank lines after method docstring.
Parameters
----------
modified_tokens: list
The list of tokens created from the docstring.
Returns
-------
modified_tokens: list
The list of tokens with any blank lines following a method
docstring removed.
"""
with contextlib.suppress(IndexError):
if (
modified_tokens[-1][4] == "\n"
and modified_tokens[-2][4].strip() == '"""'
and modified_tokens[-5][4].lstrip().startswith("def")
):
modified_tokens.pop(-1)
return modified_tokens

def _do_strip_docstring(self, docstring: str) -> Tuple[str, str]:
"""Return contents of docstring and opening quote type.
Expand Down
47 changes: 47 additions & 0 deletions tests/test_format_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,53 @@ class TestClass:
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_strip_blank_line_after_method_docstring(
self,
test_args,
args,
):
"""Strip any newlines after a method docstring.
See issue #130 and requirement PEP_257_4.4.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

docstring = '''\
class TestClass:
"""This is a class docstring."""
def test_method(self):
"""This is a method docstring.
With a long description followed by two blank lines.
"""
pass
'''
assert docstring == uut._do_format_code(
'''\
class TestClass:
"""This is a class docstring."""
def test_method(self):
"""This is a method docstring.
With a long description followed by two blank lines.
"""
pass
'''
)


class TestFormatCodeRanges:
"""Class for testing _format_code() with the line_range or length_range
Expand Down

0 comments on commit 5bae271

Please sign in to comment.