Skip to content

Commit 3eb6241

Browse files
authored
fix: summary with back ticks and sphinx field names with periods (#248)
* fix: not formatting docstrings with backticks in summary * test: for summary with backticks * fix: not recognizing sphinx field names with periods * test: for recognizing sphinx field names with periods
1 parent f40d7ab commit 3eb6241

File tree

7 files changed

+90
-8
lines changed

7 files changed

+90
-8
lines changed

src/docformatter/format.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ def _do_format_docstring( # noqa PLR0911
479479
self.args.rest_section_adorns,
480480
self.args.style,
481481
)
482-
or _syntax.do_find_directives(summary)
483482
or _syntax.do_find_links(summary)
484483
):
485484
# Something is probably not right with the splitting.

src/docformatter/syntax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
REST_REGEX = r"((\.{2}|`{2}) ?[\w.~-]+(:{2}|`{2})?[\w ]*?|`[\w.~]+`)"
6060
"""Regular expression to use for finding reST directives."""
6161

62-
SPHINX_REGEX = r":[a-zA-Z0-9_\-() ]*:"
62+
SPHINX_REGEX = r":[a-zA-Z0-9_\-(). ]*:"
6363
"""Regular expression to use for finding Sphinx-style field lists."""
6464

6565
URL_PATTERNS = (

tests/_data/string_files/do_format_code.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,14 @@ from typing import Iterator
302302
"""Don't remove this comment, it's cool."""
303303
IMPORTANT_CONSTANT = "potato"
304304
'''
305+
306+
[issue_243]
307+
instring='''def foo(bar):
308+
"""Return `foo` using `bar`. Description."""
309+
'''
310+
outstring='''def foo(bar):
311+
"""Return `foo` using `bar`.
312+
313+
Description.
314+
"""
315+
'''

tests/_data/string_files/format_sphinx.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,14 @@ outstring='''"""Summary.
212212
213213
:param param: asdf
214214
"""'''
215+
216+
[issue_245]
217+
instring='''"""Some f.
218+
:param a: Some param.
219+
:raises my.package.MyReallySrsError: Bad things happened.
220+
"""'''
221+
outstring='''"""Some f.
222+
223+
:param a: Some param.
224+
:raises my.package.MyReallySrsError: Bad things happened.
225+
"""'''

tests/_data/string_files/format_urls.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ instring='''"""Locate and call the ``mpm`` CLI.
227227
"""'''
228228
outstring='''"""Locate and call the ``mpm`` CLI.
229229
230-
The output must supports both `Xbar dialect
231-
<https://github.com/matryer/xbar-plugins/blob/main/CONTRIBUTING.md#plugin-api>`_
230+
The output must supports both
231+
`Xbar dialect <https://github.com/matryer/xbar-plugins/blob/main/CONTRIBUTING.md#plugin-api>`_
232232
and `SwiftBar dialect <https://github.com/swiftbar/SwiftBar#plugin-api>`_.
233233
"""'''
234234

@@ -288,8 +288,7 @@ def sub_func_test():
288288

289289
[issue_157_8]
290290
instring='''def mixed_links():
291-
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI on the
292-
system.
291+
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI on the system.
293292
294293
Once ``mpm`` is located, we can rely on it to produce the main output of the plugin.
295294
@@ -331,8 +330,8 @@ By default we choose to exclude:
331330
options set.
332331
"""'''
333332
outstring='''def mixed_links():
334-
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI on the
335-
system.
333+
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI
334+
on the system.
336335
337336
Once ``mpm`` is located, we can rely on it to produce the main output of the plugin.
338337

tests/formatter/test_do_format_code.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,28 @@ def test_format_code_keep_newline_after_import(
420420
assert outstring == uut._do_format_code(
421421
instring,
422422
)
423+
424+
@pytest.mark.unit
425+
@pytest.mark.parametrize("args", [[""]])
426+
def test_format_code_with_backtick_in_summary(
427+
self,
428+
test_args,
429+
args,
430+
):
431+
"""Format docstring with summary containing backticks.
432+
433+
See issue #243.
434+
"""
435+
uut = Formatter(
436+
test_args,
437+
sys.stderr,
438+
sys.stdin,
439+
sys.stdout,
440+
)
441+
442+
instring = self.TEST_STRINGS["issue_243"]["instring"]
443+
outstring = self.TEST_STRINGS["issue_243"]["outstring"]
444+
445+
assert outstring == uut._do_format_code(
446+
instring,
447+
)

tests/formatter/test_format_sphinx.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,40 @@ def test_format_docstring_sphinx_style_field_body_is_a_link(
359359
INDENTATION,
360360
instring,
361361
)
362+
363+
@pytest.mark.unit
364+
@pytest.mark.parametrize(
365+
"args",
366+
[
367+
[
368+
"--wrap-descriptions",
369+
"88",
370+
"--wrap-summaries",
371+
"88",
372+
"",
373+
]
374+
],
375+
)
376+
def test_format_docstring_sphinx_style_field_name_has_periods(
377+
self,
378+
test_args,
379+
args,
380+
):
381+
"""Should format sphinx field names containing a period.
382+
383+
See issue #245.
384+
"""
385+
uut = Formatter(
386+
test_args,
387+
sys.stderr,
388+
sys.stdin,
389+
sys.stdout,
390+
)
391+
392+
instring = self.TEST_STRINGS["issue_245"]["instring"]
393+
outstring = self.TEST_STRINGS["issue_245"]["outstring"]
394+
395+
assert outstring == uut._do_format_docstring(
396+
INDENTATION,
397+
instring,
398+
)

0 commit comments

Comments
 (0)