From 4a20a29cfc89dca11797444b50214ddce21fd615 Mon Sep 17 00:00:00 2001 From: Tobias Klare Date: Thu, 17 Oct 2024 14:21:47 +0200 Subject: [PATCH] fix: extend list of sphinx fields (#271) --- src/docformatter/syntax.py | 19 ++++++++++- tests/_data/string_files/format_sphinx.toml | 33 ++++++++++++++++++ tests/formatter/test_format_sphinx.py | 38 +++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/docformatter/syntax.py b/src/docformatter/syntax.py index 8981e87..4357038 100644 --- a/src/docformatter/syntax.py +++ b/src/docformatter/syntax.py @@ -59,7 +59,24 @@ 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|" + "yield|" + "var" +) + +SPHINX_REGEX = rf":({SPHINX_FIELD_PATTERNS})[a-zA-Z0-9_\-.() ]*:" """Regular expression to use for finding Sphinx-style field lists.""" URL_PATTERNS = ( diff --git a/tests/_data/string_files/format_sphinx.toml b/tests/_data/string_files/format_sphinx.toml index 38eefaa..4f78497 100644 --- a/tests/_data/string_files/format_sphinx.toml +++ b/tests/_data/string_files/format_sphinx.toml @@ -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. + """''' diff --git a/tests/formatter/test_format_sphinx.py b/tests/formatter/test_format_sphinx.py index 03480db..c8fc1b9 100644 --- a/tests/formatter/test_format_sphinx.py +++ b/tests/formatter/test_format_sphinx.py @@ -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, + )