Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extend list of sphinx fields (#271) #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)