Skip to content

Commit

Permalink
[Pylint] @Property dont require :return: (#6400)
Browse files Browse the repository at this point in the history
* if it is a @Property dont require :returns:

* test line update
  • Loading branch information
l0lawrence authored Jun 27, 2023
1 parent bf65ed2 commit 1f51515
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,8 @@ def check_return(self, node):
:param node: ast.FunctionDef
:return: None
"""
# Get decorators on the function
function_decorators = node.decoratornames()
try:
returns = next(node.infer_call_result()).as_string()
if returns == "None":
Expand All @@ -1396,7 +1398,8 @@ def check_return(self, node):
if line.startswith("rtype"):
has_rtype = True

if has_return is False:
# If is an @property decorator, don't require :return: as it is repetitive
if has_return is False and "builtins.property" not in function_decorators:
self.add_message(
msgid="docstring-missing-return", node=node, confidence=None
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3441,3 +3441,43 @@ def function_foo(*x, y, z):
)
):
self.checker.visit_functiondef(node)

def test_docstring_property_decorator(self):
node = astroid.extract_node(
"""
from typing import Dict
@property
def function_foo(self) -> Dict[str,str]:
'''The current headers collection.
:rtype: dict[str, str]
'''
return {"hello": "world"}
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(node)

def test_docstring_no_property_decorator(self):
node = astroid.extract_node(
"""
from typing import Dict
def function_foo(self) -> Dict[str,str]:
'''The current headers collection.
:rtype: dict[str, str]
'''
return {"hello": "world"}
"""
)
with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="docstring-missing-return",
line=3,
args=None,
node=node,
col_offset=0,
end_line=3,
end_col_offset=16
),
):
self.checker.visit_functiondef(node)

0 comments on commit 1f51515

Please sign in to comment.