Skip to content

Commit

Permalink
fix: removing newline between Sphinx field lists (#237)
Browse files Browse the repository at this point in the history
* fix: removing blank line between field lists

* fix: removing newline between Sphinx directives
  • Loading branch information
weibullguy committed Jun 9, 2023
1 parent f9c7d4c commit 7502a15
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
project = "docformatter"
copyright = "2022-2023, Steven Myint"
author = "Steven Myint"
release = "1.7.2"
release = "1.7.3-alpha"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "docformatter"
version = "1.7.2"
version = "1.7.3-alpha"
description = "Formats docstrings to follow PEP 257"
authors = ["Steven Myint"]
maintainers = [
Expand Down
2 changes: 1 addition & 1 deletion src/docformatter/__pkginfo__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
# SOFTWARE.
"""Package information for docformatter."""

__version__ = "1.7.2"
__version__ = "1.7.3-alpha"
52 changes: 28 additions & 24 deletions src/docformatter/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@


def description_to_list(
text: str,
description: str,
indentation: str,
wrap_length: int,
) -> List[str]:
"""Convert the description to a list of wrap length lines.
Parameters
----------
text : str
description : str
The docstring description.
indentation : str
The indentation (number of spaces or tabs) to place in front of each
Expand All @@ -180,38 +180,43 @@ def description_to_list(
Returns
-------
_lines : list
A list containing each line of the description with any links put
back together.
_wrapped_lines : list
A list containing each line of the description wrapped at wrap_length.
"""
# This is a description containing only one paragraph.
if len(re.findall(r"\n\n", text)) <= 0:
if len(re.findall(r"\n\n", description)) <= 0:
return textwrap.wrap(
textwrap.dedent(text),
textwrap.dedent(description),
width=wrap_length,
initial_indent=indentation,
subsequent_indent=indentation,
)

# This is a description containing multiple paragraphs.
_lines = []
for _line in text.split("\n\n"):
_text = textwrap.wrap(
_wrapped_lines = []
for _line in description.split("\n\n"):
_wrapped_line = textwrap.wrap(
textwrap.dedent(_line),
width=wrap_length,
initial_indent=indentation,
subsequent_indent=indentation,
)

if _text:
_lines.extend(_text)
_lines.append("")
if _wrapped_line:
_wrapped_lines.extend(_wrapped_line)
_wrapped_lines.append("")

with contextlib.suppress(IndexError):
if not _lines[-1] and not _lines[-2]:
_lines.pop(-1)
if not _wrapped_lines[-1] and not _wrapped_lines[-2]:
_wrapped_lines.pop(-1)

return _lines
if (
description[-len(indentation) - 1 : -len(indentation)] == "\n"
and description[-len(indentation) - 2 : -len(indentation)] != "\n\n"
):
_wrapped_lines.pop(-1)

return _wrapped_lines


def do_clean_url(url: str, indentation: str) -> str:
Expand Down Expand Up @@ -479,13 +484,10 @@ def do_wrap_field_lists( # noqa: PLR0913
text,
field_idx,
_idx,
).strip()
)

if len(f"{_field_name} {_field_body}") <= (wrap_length - len(indentation)):
if _field_body.startswith("`") or not _field_body:
_field = f"{_field_name}{_field_body}"
else:
_field = f"{_field_name} {_field_body}"
if len(f"{_field_name}{_field_body}") <= (wrap_length - len(indentation)):
_field = f"{_field_name}{_field_body}"
lines.append(f"{indentation}{_field}")
else:
lines.extend(
Expand Down Expand Up @@ -855,8 +857,10 @@ def _do_join_field_body(text, field_idx, idx):
[_line.strip() for _line in _field_body.splitlines()]
).strip()

if _field_body:
if not _field_body.startswith("`"):
_field_body = f" {_field_body}"
if text[field_idx[idx][1] : field_idx[idx][1] + 1] == "\n":
_field_body = "\n"

return _field_body

Expand Down Expand Up @@ -886,7 +890,7 @@ def _do_wrap_field(field_name, field_body, indentation, wrap_length):
_subsequent = 2 * indentation

_wrapped_field = textwrap.wrap(
textwrap.dedent(f"{field_name} {field_body.strip()}"),
textwrap.dedent(f"{field_name}{field_body}"),
width=wrap_length,
initial_indent=indentation,
subsequent_indent=_subsequent,
Expand Down
49 changes: 48 additions & 1 deletion tests/test_format_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,7 +2150,7 @@ def test_format_docstring_sphinx_style_field_body_is_a_link(
):
"""Should not add a space after the field name when the body is a link.
See docformatter_10.4.3.1, issue #229, and issue #230.
See docformatter_10.4.3.1, issue #229, issue #230, issue #234, and issue #235.
"""
uut = Formatter(
test_args,
Expand Down Expand Up @@ -2199,6 +2199,52 @@ def test_format_docstring_sphinx_style_field_body_is_a_link(
)
)

assert (
(
'''\
"""CC.
:math:`f(0) = 1`. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX
"""\
'''
)
== uut._do_format_docstring(
INDENTATION,
'''\
"""CC.
:math:`f(0) = 1`. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX
"""\
''',
)
)

assert (
(
'''\
"""CC.
C.
C,
:math:`[0, 1]`.
"""\
'''
)
== uut._do_format_docstring(
INDENTATION,
'''\
"""CC.
C.
C,
:math:`[0, 1]`.
"""\
''',
)
)

@pytest.mark.unit
@pytest.mark.parametrize(
"args",
Expand Down Expand Up @@ -2234,6 +2280,7 @@ def test_format_docstring_sphinx_style_field_body_is_blank(
"""Add trackers to a torrent.
:raises NotFound404Error:
:param torrent_hash: hash for torrent
:param urls: tracker URLs to add to torrent
:return: None
Expand Down

0 comments on commit 7502a15

Please sign in to comment.