Skip to content

Commit 55f6169

Browse files
committed
fix: extend list of sphinx fields (#271)
1 parent eb1df34 commit 55f6169

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

Diff for: src/docformatter/syntax.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,25 @@
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":(param|raises|return|rtype|type|yield)[a-zA-Z0-9_\-.() ]*:"
62+
# Complete list:
63+
# https://www.sphinx-doc.org/en/master/usage/domains/python.html#info-field-lists
64+
SPHINX_FIELD_PATTERNS = (
65+
"arg|"
66+
"cvar|"
67+
"except|"
68+
"ivar|"
69+
"key|"
70+
"meta|"
71+
"param|"
72+
"raise|"
73+
"return|"
74+
"rtype|"
75+
"type|"
76+
"var|"
77+
"yield"
78+
)
79+
80+
SPHINX_REGEX = rf":({SPHINX_FIELD_PATTERNS})[a-zA-Z0-9_\-.() ]*:"
6381
"""Regular expression to use for finding Sphinx-style field lists."""
6482

6583
URL_PATTERNS = (

Diff for: tests/_data/string_files/format_sphinx.toml

+33
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,36 @@ outstring='''"""
250250
:param caplog: Pytest caplog fixture.
251251
:yield: Until test complete, then run cleanup.
252252
"""'''
253+
254+
[issue_271]
255+
instring='''"""
256+
My test fixture.
257+
258+
:ivar id: A unique identifier for the element, automatically generated upon instantiation.
259+
:vartype id: str
260+
:ivar created: Timestamp when the element was created, defaults to the current time.
261+
:vartype created: datetime
262+
:cvar modified: Timestamp when the element was last modified, can be None if not modified.
263+
:vartype modified: Optional[datetime]
264+
:cvar in_project: List of projects this element is part of. Direct modification is restricted.
265+
:vartype in_project: list[Project]
266+
:param caplog: Pytest caplog fixture.
267+
:yield: Until test complete, then run cleanup.
268+
"""'''
269+
outstring='''"""
270+
My test fixture.
271+
272+
:ivar id: A unique identifier for the element, automatically generated upon
273+
instantiation.
274+
:vartype id: str
275+
:ivar created: Timestamp when the element was created, defaults to the current time.
276+
:vartype created: datetime
277+
:cvar modified: Timestamp when the element was last modified, can be None if not
278+
modified.
279+
:vartype modified: Optional[datetime]
280+
:cvar in_project: List of projects this element is part of. Direct modification is
281+
restricted.
282+
:vartype in_project: list[Project]
283+
:param caplog: Pytest caplog fixture.
284+
:yield: Until test complete, then run cleanup.
285+
"""'''

Diff for: tests/formatter/test_format_sphinx.py

+38
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,41 @@ def test_format_docstring_sphinx_style_recognize_yield(
472472
INDENTATION,
473473
instring,
474474
)
475+
476+
@pytest.mark.unit
477+
@pytest.mark.parametrize(
478+
"args",
479+
[
480+
[
481+
"--wrap-descriptions",
482+
"88",
483+
"--wrap-summaries",
484+
"88",
485+
"--pre-summary-newline",
486+
"",
487+
]
488+
],
489+
)
490+
def test_format_docstring_sphinx_style_recognize_more_sphinx_fields(
491+
self,
492+
test_args,
493+
args,
494+
):
495+
"""Should identify more sphinx field.
496+
497+
See issue #271
498+
"""
499+
uut = Formatter(
500+
test_args,
501+
sys.stderr,
502+
sys.stdin,
503+
sys.stdout,
504+
)
505+
506+
instring = self.TEST_STRINGS["issue_271"]["instring"]
507+
outstring = self.TEST_STRINGS["issue_271"]["outstring"]
508+
509+
assert outstring == uut._do_format_docstring(
510+
INDENTATION,
511+
instring,
512+
)

0 commit comments

Comments
 (0)