Skip to content

Commit

Permalink
fix: summary with back ticks and sphinx field names with periods (#248)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
weibullguy committed Jul 5, 2023
1 parent f40d7ab commit 3eb6241
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/docformatter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ def _do_format_docstring( # noqa PLR0911
self.args.rest_section_adorns,
self.args.style,
)
or _syntax.do_find_directives(summary)
or _syntax.do_find_links(summary)
):
# Something is probably not right with the splitting.
Expand Down
2 changes: 1 addition & 1 deletion src/docformatter/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
REST_REGEX = r"((\.{2}|`{2}) ?[\w.~-]+(:{2}|`{2})?[\w ]*?|`[\w.~]+`)"
"""Regular expression to use for finding reST directives."""

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

URL_PATTERNS = (
Expand Down
11 changes: 11 additions & 0 deletions tests/_data/string_files/do_format_code.toml
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,14 @@ from typing import Iterator
"""Don't remove this comment, it's cool."""
IMPORTANT_CONSTANT = "potato"
'''

[issue_243]
instring='''def foo(bar):
"""Return `foo` using `bar`. Description."""
'''
outstring='''def foo(bar):
"""Return `foo` using `bar`.
Description.
"""
'''
11 changes: 11 additions & 0 deletions tests/_data/string_files/format_sphinx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,14 @@ outstring='''"""Summary.
:param param: asdf
"""'''

[issue_245]
instring='''"""Some f.
:param a: Some param.
:raises my.package.MyReallySrsError: Bad things happened.
"""'''
outstring='''"""Some f.
:param a: Some param.
:raises my.package.MyReallySrsError: Bad things happened.
"""'''
11 changes: 5 additions & 6 deletions tests/_data/string_files/format_urls.toml
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ instring='''"""Locate and call the ``mpm`` CLI.
"""'''
outstring='''"""Locate and call the ``mpm`` CLI.
The output must supports both `Xbar dialect
<https://github.com/matryer/xbar-plugins/blob/main/CONTRIBUTING.md#plugin-api>`_
The output must supports both
`Xbar dialect <https://github.com/matryer/xbar-plugins/blob/main/CONTRIBUTING.md#plugin-api>`_
and `SwiftBar dialect <https://github.com/swiftbar/SwiftBar#plugin-api>`_.
"""'''

Expand Down Expand Up @@ -288,8 +288,7 @@ def sub_func_test():

[issue_157_8]
instring='''def mixed_links():
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI on the
system.
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI on the system.
Once ``mpm`` is located, we can rely on it to produce the main output of the plugin.
Expand Down Expand Up @@ -331,8 +330,8 @@ By default we choose to exclude:
options set.
"""'''
outstring='''def mixed_links():
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI on the
system.
"""Implements the minimal code necessary to locate and call the ``mpm`` CLI
on the system.
Once ``mpm`` is located, we can rely on it to produce the main output of the plugin.
Expand Down
25 changes: 25 additions & 0 deletions tests/formatter/test_do_format_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,28 @@ def test_format_code_keep_newline_after_import(
assert outstring == uut._do_format_code(
instring,
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_with_backtick_in_summary(
self,
test_args,
args,
):
"""Format docstring with summary containing backticks.
See issue #243.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

instring = self.TEST_STRINGS["issue_243"]["instring"]
outstring = self.TEST_STRINGS["issue_243"]["outstring"]

assert outstring == uut._do_format_code(
instring,
)
37 changes: 37 additions & 0 deletions tests/formatter/test_format_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,40 @@ def test_format_docstring_sphinx_style_field_body_is_a_link(
INDENTATION,
instring,
)

@pytest.mark.unit
@pytest.mark.parametrize(
"args",
[
[
"--wrap-descriptions",
"88",
"--wrap-summaries",
"88",
"",
]
],
)
def test_format_docstring_sphinx_style_field_name_has_periods(
self,
test_args,
args,
):
"""Should format sphinx field names containing a period.
See issue #245.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

instring = self.TEST_STRINGS["issue_245"]["instring"]
outstring = self.TEST_STRINGS["issue_245"]["outstring"]

assert outstring == uut._do_format_docstring(
INDENTATION,
instring,
)

0 comments on commit 3eb6241

Please sign in to comment.