From 1f51515aaefd5a0dbb4ecf92f8f6068cba1ac782 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Tue, 27 Jun 2023 08:09:49 -0700 Subject: [PATCH] [Pylint] @property dont require :return: (#6400) * if it is a @property dont require :returns: * test line update --- .../pylint_guidelines_checker.py | 5 ++- .../tests/test_pylint_custom_plugins.py | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py index b8eb9a2ecd2..124caeb3fdf 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/pylint_guidelines_checker.py @@ -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": @@ -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 ) diff --git a/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py b/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py index 4c17f0c5c56..a2f9d2dc1b4 100644 --- a/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py +++ b/tools/pylint-extensions/azure-pylint-guidelines-checker/tests/test_pylint_custom_plugins.py @@ -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) \ No newline at end of file