Skip to content

Commit 8e15c1d

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Fix accept-no-yields/return-doc for partially correct docstrings
1 parent 7976857 commit 8e15c1d

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Release date: TBA
1919

2020
* Fix exception when pyreverse parses ``property function`` of a class.
2121

22+
* Fix ``accept-no-yields-doc`` and ``accept-no-return-doc`` not allowing missing ``yield`` or
23+
``return`` documentation when a docstring is partially correct
24+
25+
Closes #5223
26+
2227
* Add an optional extension ``consider-using-any-or-all`` : Emitted when a ``for`` loop only
2328
produces a boolean and could be replaced by ``any`` or ``all`` using a generator. Also suggests
2429
a suitable any or all statement.

doc/whatsnew/2.12.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ Other Changes
7777

7878
Closes #5178
7979

80+
* Fix ``accept-no-yields-doc`` and ``accept-no-return-doc`` not allowing missing ``yield`` or
81+
``return`` documentation when a docstring is partially correct
82+
83+
Closes #5223
84+
8085
* Fix ``simplify-boolean-expression`` when condition can be inferred as False.
8186

8287
Closes #5200

pylint/extensions/docparams.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,14 @@ def visit_return(self, node: nodes.Return) -> None:
322322
if not utils.returns_something(node):
323323
return
324324

325+
if self.config.accept_no_return_doc:
326+
return
327+
325328
func_node = node.frame()
326329
if not isinstance(func_node, astroid.FunctionDef):
327330
return
328331

329332
doc = utils.docstringify(func_node.doc, self.config.default_docstring_type)
330-
if not doc.is_valid() and self.config.accept_no_return_doc:
331-
return
332333

333334
is_property = checker_utils.decorated_with_property(func_node)
334335

@@ -342,13 +343,14 @@ def visit_return(self, node: nodes.Return) -> None:
342343
self.add_message("missing-return-type-doc", node=func_node)
343344

344345
def visit_yield(self, node: nodes.Yield) -> None:
346+
if self.config.accept_no_yields_doc:
347+
return
348+
345349
func_node = node.frame()
346350
if not isinstance(func_node, astroid.FunctionDef):
347351
return
348352

349353
doc = utils.docstringify(func_node.doc, self.config.default_docstring_type)
350-
if not doc.is_valid() and self.config.accept_no_yields_doc:
351-
return
352354

353355
if doc.supports_yields:
354356
doc_has_yields = doc.has_yields()

tests/extensions/test_check_docs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ def foo(self) -> int: #@
18861886
with self.assertNoMessages():
18871887
self.checker.visit_return(node)
18881888

1889+
@set_config(accept_no_return_doc="no")
18891890
def test_finds_missing_property_return_type_sphinx(self) -> None:
18901891
"""Example of a property having missing return documentation in
18911892
a Sphinx style docstring
@@ -1929,6 +1930,7 @@ def foo(self) -> int: #@
19291930
with self.assertNoMessages():
19301931
self.checker.visit_return(node)
19311932

1933+
@set_config(accept_no_return_doc="no")
19321934
def test_finds_missing_property_return_type_google(self) -> None:
19331935
"""Example of a property having return documentation in
19341936
a Google style docstring
@@ -1952,6 +1954,7 @@ def foo(self): #@
19521954
):
19531955
self.checker.visit_return(node)
19541956

1957+
@set_config(accept_no_return_doc="no")
19551958
def test_finds_missing_property_return_type_numpy(self) -> None:
19561959
"""Example of a property having return documentation in
19571960
a numpy style docstring
@@ -1977,6 +1980,7 @@ def foo(self): #@
19771980
):
19781981
self.checker.visit_return(node)
19791982

1983+
@set_config(accept_no_return_doc="no")
19801984
def test_ignores_non_property_return_type_sphinx(self) -> None:
19811985
"""Example of a class function trying to use `type` as return
19821986
documentation in a Sphinx style docstring
@@ -1998,6 +2002,7 @@ def foo(self): #@
19982002
):
19992003
self.checker.visit_return(node)
20002004

2005+
@set_config(accept_no_return_doc="no")
20012006
def test_ignores_non_property_return_type_google(self) -> None:
20022007
"""Example of a class function trying to use `type` as return
20032008
documentation in a Google style docstring
@@ -2021,6 +2026,7 @@ def foo(self): #@
20212026
):
20222027
self.checker.visit_return(node)
20232028

2029+
@set_config(accept_no_return_doc="no")
20242030
def test_ignores_non_property_return_type_numpy(self) -> None:
20252031
"""Example of a class function trying to use `type` as return
20262032
documentation in a numpy style docstring
@@ -2046,6 +2052,7 @@ def foo(self): #@
20462052
):
20472053
self.checker.visit_return(node)
20482054

2055+
@set_config(accept_no_return_doc="no")
20492056
def test_non_property_annotation_return_type_numpy(self) -> None:
20502057
"""Example of a class function trying to use `type` as return
20512058
documentation in a numpy style docstring

tests/extensions/test_check_return_docs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def my_func(self):
6868
with self.assertNoMessages():
6969
self.checker.visit_return(return_node)
7070

71+
@set_config(accept_no_return_doc="no")
7172
def test_warn_partial_sphinx_returns(self) -> None:
7273
node = astroid.extract_node(
7374
'''
@@ -100,6 +101,7 @@ def my_func(self) -> bool:
100101
with self.assertNoMessages():
101102
self.checker.visit_return(return_node)
102103

104+
@set_config(accept_no_return_doc="no")
103105
def test_warn_partial_sphinx_returns_type(self) -> None:
104106
node = astroid.extract_node(
105107
'''
@@ -117,6 +119,7 @@ def my_func(self):
117119
):
118120
self.checker.visit_return(return_node)
119121

122+
@set_config(accept_no_return_doc="no")
120123
def test_warn_missing_sphinx_returns(self) -> None:
121124
node = astroid.extract_node(
122125
'''
@@ -136,6 +139,7 @@ def my_func(self, doc_type):
136139
):
137140
self.checker.visit_return(return_node)
138141

142+
@set_config(accept_no_return_doc="no")
139143
def test_warn_partial_google_returns(self) -> None:
140144
node = astroid.extract_node(
141145
'''
@@ -154,6 +158,7 @@ def my_func(self):
154158
):
155159
self.checker.visit_return(return_node)
156160

161+
@set_config(accept_no_return_doc="no")
157162
def test_warn_partial_google_returns_type(self) -> None:
158163
node = astroid.extract_node(
159164
'''
@@ -172,6 +177,7 @@ def my_func(self):
172177
):
173178
self.checker.visit_return(return_node)
174179

180+
@set_config(accept_no_return_doc="no")
175181
def test_warn_missing_google_returns(self) -> None:
176182
node = astroid.extract_node(
177183
'''
@@ -191,6 +197,7 @@ def my_func(self, doc_type):
191197
):
192198
self.checker.visit_return(return_node)
193199

200+
@set_config(accept_no_return_doc="no")
194201
def test_warn_partial_numpy_returns_type(self) -> None:
195202
node = astroid.extract_node(
196203
'''
@@ -215,6 +222,7 @@ def my_func(self, doc_type):
215222
):
216223
self.checker.visit_return(return_node)
217224

225+
@set_config(accept_no_return_doc="no")
218226
def test_warn_missing_numpy_returns(self) -> None:
219227
node = astroid.extract_node(
220228
'''
@@ -441,6 +449,7 @@ def my_func(self):
441449
with self.assertNoMessages():
442450
self.checker.visit_return(return_node)
443451

452+
@set_config(accept_no_return_doc="no")
444453
def test_warns_sphinx_return_list_of_custom_class_without_description(self) -> None:
445454
node = astroid.extract_node(
446455
'''
@@ -458,6 +467,7 @@ def my_func(self):
458467
):
459468
self.checker.visit_return(return_node)
460469

470+
@set_config(accept_no_return_doc="no")
461471
def test_warns_google_return_list_of_custom_class_without_description(self) -> None:
462472
node = astroid.extract_node(
463473
'''
@@ -476,6 +486,7 @@ def my_func(self):
476486
):
477487
self.checker.visit_return(return_node)
478488

489+
@set_config(accept_no_return_doc="no")
479490
def test_warns_numpy_return_list_of_custom_class_without_description(self) -> None:
480491
node = astroid.extract_node(
481492
'''

tests/extensions/test_check_yields_docs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def my_func(self):
6464
with self.assertNoMessages():
6565
self.checker.visit_yield(yield_node)
6666

67+
@set_config(accept_no_yields_doc="no")
6768
def test_warn_partial_sphinx_yields(self) -> None:
6869
node = astroid.extract_node(
6970
'''
@@ -81,6 +82,7 @@ def my_func(self):
8182
):
8283
self.checker.visit_yield(yield_node)
8384

85+
@set_config(accept_no_yields_doc="no")
8486
def test_warn_partial_sphinx_yields_type(self) -> None:
8587
node = astroid.extract_node(
8688
'''
@@ -98,6 +100,7 @@ def my_func(self):
98100
):
99101
self.checker.visit_yield(yield_node)
100102

103+
@set_config(accept_no_yields_doc="no")
101104
def test_warn_missing_sphinx_yields(self) -> None:
102105
node = astroid.extract_node(
103106
'''
@@ -117,6 +120,7 @@ def my_func(self, doc_type):
117120
):
118121
self.checker.visit_yield(yield_node)
119122

123+
@set_config(accept_no_yields_doc="no")
120124
def test_warn_partial_google_yields(self) -> None:
121125
node = astroid.extract_node(
122126
'''
@@ -135,6 +139,7 @@ def my_func(self):
135139
):
136140
self.checker.visit_yield(yield_node)
137141

142+
@set_config(accept_no_yields_doc="no")
138143
def test_warn_partial_google_yields_type(self) -> None:
139144
node = astroid.extract_node(
140145
'''
@@ -153,6 +158,7 @@ def my_func(self):
153158
):
154159
self.checker.visit_yield(yield_node)
155160

161+
@set_config(accept_no_yields_doc="no")
156162
def test_warn_missing_google_yields(self) -> None:
157163
node = astroid.extract_node(
158164
'''
@@ -172,6 +178,7 @@ def my_func(self, doc_type):
172178
):
173179
self.checker.visit_yield(yield_node)
174180

181+
@set_config(accept_no_yields_doc="no")
175182
def test_warn_missing_numpy_yields(self) -> None:
176183
node = astroid.extract_node(
177184
'''
@@ -334,6 +341,7 @@ def my_func(self):
334341
with self.assertNoMessages():
335342
self.checker.visit_yield(yield_node)
336343

344+
@set_config(accept_no_yields_doc="no")
337345
def test_warns_sphinx_yield_list_of_custom_class_without_description(self) -> None:
338346
node = astroid.extract_node(
339347
'''
@@ -351,6 +359,7 @@ def my_func(self):
351359
):
352360
self.checker.visit_yield(yield_node)
353361

362+
@set_config(accept_no_yields_doc="no")
354363
def test_warns_google_yield_list_of_custom_class_without_description(self) -> None:
355364
node = astroid.extract_node(
356365
'''
@@ -369,6 +378,7 @@ def my_func(self):
369378
):
370379
self.checker.visit_yield(yield_node)
371380

381+
@set_config(accept_no_yields_doc="no")
372382
def test_warns_numpy_yield_list_of_custom_class_without_description(self) -> None:
373383
node = astroid.extract_node(
374384
'''

0 commit comments

Comments
 (0)