Skip to content

Commit

Permalink
fix: extend list of sphinx fields (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Klare authored and finswimmer committed Oct 17, 2024
1 parent eb1df34 commit 2694553
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/docformatter/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,25 @@
REST_REGEX = r"((\.{2}|`{2}) ?[\w.~-]+(:{2}|`{2})?[\w ]*?|`[\w.~]+`)"
"""Regular expression to use for finding reST directives."""

SPHINX_REGEX = r":(param|raises|return|rtype|type|yield)[a-zA-Z0-9_\-.() ]*:"
# Complete list:
# https://www.sphinx-doc.org/en/master/usage/domains/python.html#info-field-lists
SPHINX_FIELD_PATTERNS = (
"arg|"
"cvar|"
"except|"
"ivar|"
"key|"
"meta|"
"param|"
"raise|"
"return|"
"rtype|"
"type|"
"var|"
"yield"
)

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

URL_PATTERNS = (
Expand Down
33 changes: 33 additions & 0 deletions tests/_data/string_files/format_sphinx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,36 @@ outstring='''"""
:param caplog: Pytest caplog fixture.
:yield: Until test complete, then run cleanup.
"""'''

[issue_271]
instring='''"""
My test fixture.
:ivar id: A unique identifier for the element, automatically generated upon instantiation.
:vartype id: str
:ivar created: Timestamp when the element was created, defaults to the current time.
:vartype created: datetime
:cvar modified: Timestamp when the element was last modified, can be None if not modified.
:vartype modified: Optional[datetime]
:cvar in_project: List of projects this element is part of. Direct modification is restricted.
:vartype in_project: list[Project]
:param caplog: Pytest caplog fixture.
:yield: Until test complete, then run cleanup.
"""'''
outstring='''"""
My test fixture.
:ivar id: A unique identifier for the element, automatically generated upon
instantiation.
:vartype id: str
:ivar created: Timestamp when the element was created, defaults to the current time.
:vartype created: datetime
:cvar modified: Timestamp when the element was last modified, can be None if not
modified.
:vartype modified: Optional[datetime]
:cvar in_project: List of projects this element is part of. Direct modification is
restricted.
:vartype in_project: list[Project]
:param caplog: Pytest caplog fixture.
:yield: Until test complete, then run cleanup.
"""'''
38 changes: 38 additions & 0 deletions tests/formatter/test_format_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,41 @@ def test_format_docstring_sphinx_style_recognize_yield(
INDENTATION,
instring,
)

@pytest.mark.unit
@pytest.mark.parametrize(
"args",
[
[
"--wrap-descriptions",
"88",
"--wrap-summaries",
"88",
"--pre-summary-newline",
"",
]
],
)
def test_format_docstring_sphinx_style_recognize_more_sphinx_fields(
self,
test_args,
args,
):
"""Should identify more sphinx field.
See issue #271
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

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

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

0 comments on commit 2694553

Please sign in to comment.