Skip to content

Commit 40cd086

Browse files
committed
DocumentationStyleBear: Add expand_one_liners
Add expand_one_liners setting which will expand one liners to multi line docstring. Closes #1856
1 parent 314dfdf commit 40cd086

5 files changed

Lines changed: 45 additions & 13 deletions

File tree

bears/documentation/DocumentationStyleBear.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ class DocumentationStyleBear(DocBaseClass, LocalBear):
2020
CAN_DETECT = {'Documentation'}
2121
CAN_FIX = {'Documentation'}
2222

23-
def process_documentation(self, parsed, allow_missing_func_desc: str=False,
24-
indent_size: int=4):
23+
def process_documentation(self,
24+
parsed,
25+
allow_missing_func_desc: str=False,
26+
indent_size: int=4,
27+
expand_one_liners: str=False):
2528
"""
2629
This fixes the parsed documentation comment.
2730
@@ -32,6 +35,8 @@ def process_documentation(self, parsed, allow_missing_func_desc: str=False,
3235
descriptions, allowing functions to start with params.
3336
:param indent_size:
3437
Number of spaces per indentation level.
38+
:param expand_one_liners:
39+
When set ``True`` this will expand one liner docstrings.
3540
:return:
3641
A tuple of fixed parsed documentation comment and warning_desc.
3742
"""
@@ -54,9 +59,13 @@ def process_documentation(self, parsed, allow_missing_func_desc: str=False,
5459
# one empty line shall follow main description (except it's empty
5560
# or no annotations follow).
5661
if main_description.desc.strip() != '':
57-
main_description = main_description._replace(
58-
desc='\n' + main_description.desc.strip() + '\n' *
59-
(1 if len(parsed) == 1 else 2))
62+
if not expand_one_liners and len(parsed) == 1:
63+
main_description = main_description._replace(
64+
desc=main_description.desc.strip())
65+
else:
66+
main_description = main_description._replace(
67+
desc='\n' + main_description.desc.strip() + '\n' *
68+
(1 if len(parsed) == 1 else 2))
6069

6170
new_metadata = [main_description]
6271
for m in metadata:
@@ -87,7 +96,7 @@ def process_documentation(self, parsed, allow_missing_func_desc: str=False,
8796

8897
def run(self, filename, file, language: str,
8998
docstyle: str='default', allow_missing_func_desc: str=False,
90-
indent_size: int=4):
99+
indent_size: int=4, expand_one_liners: str=False):
91100
"""
92101
Checks for certain in-code documentation styles.
93102
@@ -110,13 +119,16 @@ def run(self, filename, file, language: str,
110119
functions with missing descriptions, allowing
111120
functions to start with params.
112121
:param indent_size: Number of spaces per indentation level.
122+
:param expand_one_liners: When set ``True`` this will expand one liner
123+
docstrings.
113124
"""
114125

115126
for doc_comment in self.extract(file, language, docstyle):
116127
parsed = doc_comment.parse()
117128

118129
(new_metadata, warning_desc) = self.process_documentation(
119-
parsed, allow_missing_func_desc, indent_size)
130+
parsed, allow_missing_func_desc, indent_size,
131+
expand_one_liners)
120132

121133
new_comment = DocumentationComment.from_metadata(
122134
new_metadata, doc_comment.docstyle_definition,

tests/documentation/DocumentationStyleBearTest.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def load_testfile(filename):
1616
return fl.read()
1717

1818

19-
def test(test_file, expected_file):
19+
def test(test_file, expected_file, optional_setting=None):
2020
def test_function(self):
2121
test_file_content = load_testfile(test_file).splitlines(True)
2222

2323
arguments = {'language': 'python', 'docstyle': 'default'}
24-
if test_file == 'good_file3.py.test':
25-
arguments.update({'allow_missing_func_desc': 'True'})
24+
if optional_setting:
25+
arguments.update(optional_setting)
2626
section = Section('test-section')
2727
for key, value in arguments.items():
2828
section[key] = value
@@ -49,10 +49,13 @@ def test_function(self):
4949

5050
class DocumentationStyleBearTest(unittest.TestCase):
5151
test_bad1 = test('bad_file.py.test', 'bad_file.py.test.correct')
52-
test_bad2 = test('bad_file2.py.test', 'bad_file2.py.test.correct')
53-
test_bad3 = test('bad_file3.py.test', 'bad_file3.py.test.correct')
52+
test_bad2 = test('bad_file2.py.test', 'bad_file2.py.test.correct',
53+
{'expand_one_liners': 'True'})
54+
test_bad3 = test('bad_file3.py.test', 'bad_file3.py.test.correct',
55+
{'expand_one_liners': 'True'})
5456
test_bad4 = test('bad_file4.py.test', 'bad_file4.py.test.correct')
5557
test_bad5 = test('bad_file5.py.test', 'bad_file5.py.test.correct')
5658
test_good1 = test('good_file.py.test', 'good_file.py.test')
5759
test_good2 = test('good_file2.py.test', 'good_file2.py.test')
58-
test_good3 = test('good_file3.py.test', 'good_file3.py.test')
60+
test_good3 = test('good_file3.py.test', 'good_file3.py.test',
61+
{'allow_missing_func_desc': 'True'})

tests/documentation/test_files/DocumentationStyleBear/bad_file5.py.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ def docstring_improper_alignment():
77
:param x:
88
:return:
99
""" return None
10+
11+
""" This is one liner having extra whitespaces """
12+
13+
"""
14+
This is malformed one liner docstring"""
15+
16+
"""
17+
This is multi-liner docstring
18+
"""

tests/documentation/test_files/DocumentationStyleBear/bad_file5.py.test.correct

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ def docstring_improper_alignment():
77
:param x:
88
:return:
99
""" return None
10+
11+
"""This is one liner having extra whitespaces"""
12+
13+
"""This is malformed one liner docstring"""
14+
15+
"""This is multi-liner docstring"""

tests/documentation/test_files/DocumentationStyleBear/good_file3.py.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ def docstring_missing_description(dummy):
44
a function starting with `param`
55
in this case allow_missing_func_desc = True
66
"""
7+
8+
"""This is one-liner docstring"""

0 commit comments

Comments
 (0)