Skip to content

Commit 7502a15

Browse files
authored
fix: removing newline between Sphinx field lists (#237)
* fix: removing blank line between field lists * fix: removing newline between Sphinx directives
1 parent f9c7d4c commit 7502a15

File tree

5 files changed

+79
-28
lines changed

5 files changed

+79
-28
lines changed

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
project = "docformatter"
1212
copyright = "2022-2023, Steven Myint"
1313
author = "Steven Myint"
14-
release = "1.7.2"
14+
release = "1.7.3-alpha"
1515

1616
# -- General configuration ---------------------------------------------------
1717
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "docformatter"
3-
version = "1.7.2"
3+
version = "1.7.3-alpha"
44
description = "Formats docstrings to follow PEP 257"
55
authors = ["Steven Myint"]
66
maintainers = [

src/docformatter/__pkginfo__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
# SOFTWARE.
2424
"""Package information for docformatter."""
2525

26-
__version__ = "1.7.2"
26+
__version__ = "1.7.3-alpha"

src/docformatter/syntax.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@
162162

163163

164164
def description_to_list(
165-
text: str,
165+
description: str,
166166
indentation: str,
167167
wrap_length: int,
168168
) -> List[str]:
169169
"""Convert the description to a list of wrap length lines.
170170
171171
Parameters
172172
----------
173-
text : str
173+
description : str
174174
The docstring description.
175175
indentation : str
176176
The indentation (number of spaces or tabs) to place in front of each
@@ -180,38 +180,43 @@ def description_to_list(
180180
181181
Returns
182182
-------
183-
_lines : list
184-
A list containing each line of the description with any links put
185-
back together.
183+
_wrapped_lines : list
184+
A list containing each line of the description wrapped at wrap_length.
186185
"""
187186
# This is a description containing only one paragraph.
188-
if len(re.findall(r"\n\n", text)) <= 0:
187+
if len(re.findall(r"\n\n", description)) <= 0:
189188
return textwrap.wrap(
190-
textwrap.dedent(text),
189+
textwrap.dedent(description),
191190
width=wrap_length,
192191
initial_indent=indentation,
193192
subsequent_indent=indentation,
194193
)
195194

196195
# This is a description containing multiple paragraphs.
197-
_lines = []
198-
for _line in text.split("\n\n"):
199-
_text = textwrap.wrap(
196+
_wrapped_lines = []
197+
for _line in description.split("\n\n"):
198+
_wrapped_line = textwrap.wrap(
200199
textwrap.dedent(_line),
201200
width=wrap_length,
202201
initial_indent=indentation,
203202
subsequent_indent=indentation,
204203
)
205204

206-
if _text:
207-
_lines.extend(_text)
208-
_lines.append("")
205+
if _wrapped_line:
206+
_wrapped_lines.extend(_wrapped_line)
207+
_wrapped_lines.append("")
209208

210209
with contextlib.suppress(IndexError):
211-
if not _lines[-1] and not _lines[-2]:
212-
_lines.pop(-1)
210+
if not _wrapped_lines[-1] and not _wrapped_lines[-2]:
211+
_wrapped_lines.pop(-1)
213212

214-
return _lines
213+
if (
214+
description[-len(indentation) - 1 : -len(indentation)] == "\n"
215+
and description[-len(indentation) - 2 : -len(indentation)] != "\n\n"
216+
):
217+
_wrapped_lines.pop(-1)
218+
219+
return _wrapped_lines
215220

216221

217222
def do_clean_url(url: str, indentation: str) -> str:
@@ -479,13 +484,10 @@ def do_wrap_field_lists( # noqa: PLR0913
479484
text,
480485
field_idx,
481486
_idx,
482-
).strip()
487+
)
483488

484-
if len(f"{_field_name} {_field_body}") <= (wrap_length - len(indentation)):
485-
if _field_body.startswith("`") or not _field_body:
486-
_field = f"{_field_name}{_field_body}"
487-
else:
488-
_field = f"{_field_name} {_field_body}"
489+
if len(f"{_field_name}{_field_body}") <= (wrap_length - len(indentation)):
490+
_field = f"{_field_name}{_field_body}"
489491
lines.append(f"{indentation}{_field}")
490492
else:
491493
lines.extend(
@@ -855,8 +857,10 @@ def _do_join_field_body(text, field_idx, idx):
855857
[_line.strip() for _line in _field_body.splitlines()]
856858
).strip()
857859

858-
if _field_body:
860+
if not _field_body.startswith("`"):
859861
_field_body = f" {_field_body}"
862+
if text[field_idx[idx][1] : field_idx[idx][1] + 1] == "\n":
863+
_field_body = "\n"
860864

861865
return _field_body
862866

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

888892
_wrapped_field = textwrap.wrap(
889-
textwrap.dedent(f"{field_name} {field_body.strip()}"),
893+
textwrap.dedent(f"{field_name}{field_body}"),
890894
width=wrap_length,
891895
initial_indent=indentation,
892896
subsequent_indent=_subsequent,

tests/test_format_docstring.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,7 +2150,7 @@ def test_format_docstring_sphinx_style_field_body_is_a_link(
21502150
):
21512151
"""Should not add a space after the field name when the body is a link.
21522152
2153-
See docformatter_10.4.3.1, issue #229, and issue #230.
2153+
See docformatter_10.4.3.1, issue #229, issue #230, issue #234, and issue #235.
21542154
"""
21552155
uut = Formatter(
21562156
test_args,
@@ -2199,6 +2199,52 @@ def test_format_docstring_sphinx_style_field_body_is_a_link(
21992199
)
22002200
)
22012201

2202+
assert (
2203+
(
2204+
'''\
2205+
"""CC.
2206+
2207+
:math:`f(0) = 1`. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX
2208+
"""\
2209+
'''
2210+
)
2211+
== uut._do_format_docstring(
2212+
INDENTATION,
2213+
'''\
2214+
"""CC.
2215+
2216+
:math:`f(0) = 1`. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX
2217+
"""\
2218+
''',
2219+
)
2220+
)
2221+
2222+
assert (
2223+
(
2224+
'''\
2225+
"""CC.
2226+
2227+
C.
2228+
2229+
C,
2230+
:math:`[0, 1]`.
2231+
"""\
2232+
'''
2233+
)
2234+
== uut._do_format_docstring(
2235+
INDENTATION,
2236+
'''\
2237+
"""CC.
2238+
2239+
C.
2240+
2241+
C,
2242+
:math:`[0, 1]`.
2243+
"""\
2244+
''',
2245+
)
2246+
)
2247+
22022248
@pytest.mark.unit
22032249
@pytest.mark.parametrize(
22042250
"args",
@@ -2234,6 +2280,7 @@ def test_format_docstring_sphinx_style_field_body_is_blank(
22342280
"""Add trackers to a torrent.
22352281
22362282
:raises NotFound404Error:
2283+
22372284
:param torrent_hash: hash for torrent
22382285
:param urls: tracker URLs to add to torrent
22392286
:return: None

0 commit comments

Comments
 (0)