Skip to content

Commit e11ddfb

Browse files
authored
Merge pull request #4 from SingularityX-ai/SDG-581/pep-257-convention
chore: Format docstrings to adhere to PEP 257 guidelines
2 parents ab812a9 + f0bb6bd commit e11ddfb

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

CONTRIBUTION.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Contribution Guidelines
2+
3+
Thank you for your interest in contributing to our Poetry Project! We welcome all contributions and appreciate your effort. Here are the guidelines for contributing:
4+
5+
## Reporting Issues
6+
7+
If you find any bugs or issues, please report them in the issue tracker. Be sure to include details about the issue and steps to reproduce it.
8+
9+
## Submitting Changes
10+
11+
1. Fork the repository.
12+
2. Create a new branch for your changes.
13+
3. Make your changes in your branch.
14+
4. Submit a pull request with your changes.
15+
5. Ensure your pull request passes all tests.
16+
17+
## Code of Conduct
18+
19+
We expect all contributors to follow our Code of Conduct. Please review it before contributing.
20+
21+
## Writing Poetry
22+
23+
When writing poetry, please ensure it is original work. We welcome all styles and themes of poetry.
24+
25+
## Review Process
26+
27+
Once your pull request is submitted, a maintainer will review it. They may ask for changes or clarification. Once your pull request is approved, it will be merged into the main branch.
28+
29+
Thank you for your contribution!

PyDocSmith/common.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Common methods for parsing."""
22
import enum
3+
import textwrap
34
import typing as T
45

56
PARAM_KEYWORDS = {
@@ -177,7 +178,7 @@ def __init__(
177178
self.long_description = None # type: T.Optional[str]
178179
self.blank_after_short_description = False
179180
self.blank_after_long_description = False
180-
self.meta = [] # type: T.List[DocstringMeta]
181+
self.meta: T.List[DocstringMeta] = [] # type: T.List[DocstringMeta]
181182
self.style = style # type: T.Optional[DocstringStyle]
182183

183184
@property
@@ -231,3 +232,20 @@ def examples(self) -> T.List[DocstringExample]:
231232
def notes(self) -> T.List[DocstringNote]:
232233
"""Return a list of information on function notes."""
233234
return [item for item in self.meta if isinstance(item, DocstringNote)]
235+
236+
def format_docstring_to_pep257(docstring: Docstring, width: int = 72) -> Docstring:
237+
if docstring.short_description:
238+
docstring.short_description = textwrap.fill(docstring.short_description.strip(), width)
239+
if docstring.long_description:
240+
docstring.long_description = textwrap.fill(docstring.long_description.strip(), width)
241+
242+
for meta in docstring.meta:
243+
if meta.description:
244+
# Ensure meta descriptions are wrapped according to PEP 8
245+
meta.description = textwrap.fill(meta.description.strip(), width)
246+
if meta.args:
247+
# Clean up args to ensure they are stripped of extra spaces
248+
meta.args = [arg.strip() for arg in meta.args if arg.strip()]
249+
250+
return docstring
251+

PyDocSmith/parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
DocstringStyle,
1111
ParseError,
1212
RenderingStyle,
13+
format_docstring_to_pep257,
1314
)
1415

1516
_STYLE_MAP = {
@@ -116,6 +117,7 @@ def compose(
116117
style: DocstringStyle = DocstringStyle.AUTO,
117118
rendering_style: RenderingStyle = RenderingStyle.COMPACT,
118119
indent: str = " ",
120+
line_width: int = 72
119121
) -> str:
120122
"""Render a parsed docstring into docstring text.
121123
@@ -127,6 +129,10 @@ def compose(
127129
module = _STYLE_MAP[
128130
docstring.style if style == DocstringStyle.AUTO else style
129131
]
132+
133+
if line_width >= 72:
134+
docstring = format_docstring_to_pep257(docstring, width=line_width)
135+
130136
return module.compose(
131137
docstring, rendering_style=rendering_style, indent=indent
132138
)

PyDocSmith/tests/test_google.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing as T
44

55
import pytest
6-
from PyDocSmith.common import Docstring, ParseError, RenderingStyle
6+
from PyDocSmith.common import Docstring, ParseError, RenderingStyle, format_docstring_to_pep257
77
from PyDocSmith.google import (
88
GoogleParser,
99
Section,
@@ -982,6 +982,35 @@ def test_unknown_meta() -> None:
982982
assert docstring.params[1].arg_name == "arg1"
983983
assert docstring.params[1].description == "desc1"
984984

985+
def test_very_long_content_for_format_docstring_to_pep257() -> None:
986+
# currently failing
987+
"""Test parsing unknown meta. This is failing"""
988+
docstring = parse(
989+
"""A sample function
990+
991+
A function the demonstrates docstrings with very long content that should be wrapped to the next line. Additionally, the function has a very long description that should be wrapped to the next line.
992+
993+
Args:
994+
arg1 (int): The firsty arg
995+
arg2 (str): The second arg
996+
arg3 (float, optional): The third arg. Defaults to 1.0.
997+
arg4 (Optional[Dict[str, Any]], optional): The last arg. Defaults to None.
998+
arg5 (str, optional): The fifth arg. Defaults to DEFAULT_ARG5.
999+
1000+
Returns:
1001+
The args packed in a mapping
1002+
"""
1003+
)
1004+
docstring = format_docstring_to_pep257(docstring)
1005+
1006+
assert docstring.short_description == "A sample function"
1007+
assert docstring.long_description == 'A function the demonstrates docstrings with very long content that\nshould be wrapped to the next line. Additionally, the function has a\nvery long description that should be wrapped to the next line.'
1008+
1009+
assert docstring.params[0].arg_name == "arg1"
1010+
assert docstring.params[0].description == "The firsty arg"
1011+
assert docstring.params[1].arg_name == "arg2"
1012+
assert docstring.params[1].description == "The second arg"
1013+
9851014

9861015
def test_unformatted_valid_docstring() -> None:
9871016
"""
@@ -1002,6 +1031,7 @@ def test_unformatted_valid_docstring() -> None:
10021031
None
10031032
"""
10041033
)
1034+
composed_docstring = compose(docstring)
10051035
assert (
10061036
docstring.short_description == "Set the RPM controller for the object."
10071037
)

0 commit comments

Comments
 (0)