Esempio n. 1
0
def lint_returns(
        page_info: base_page.PageInfo) -> Optional[api_report_pb2.ReturnLint]:
    """"Lints the returns/yields block in the docstring.

  This linter only checks if a `Returns`/`Yields` block exists in the docstring
  if it finds `return`/`yield` keyword in the source code.

  Args:
    page_info: A `PageInfo` object containing the information of a page
      generated via the api generation.

  Returns:
    A filled `ReturnLint` proto object.
  """
    return_visitor = ReturnVisitor()

    source = get_source.get_source(page_info.py_object)
    obj_ast = get_source.get_ast(page_info.py_object)
    if obj_ast is not None:
        try:
            return_visitor.visit(obj_ast)
        except Exception:  # pylint: disable=broad-except
            pass

    keywords = ('return', 'yield')

    if source is not None and any(word in source for word in keywords):
        for item in page_info.doc.docstring_parts:
            if isinstance(item, parser.TitleBlock):
                if item.title.lower().startswith(keywords):
                    return api_report_pb2.ReturnLint(returns_defined=True)
        # If "Returns"/"Yields" word is present in the brief docstring then having
        # a separate `Returns`/`Yields` section is not needed.
        if page_info.doc.brief.lower().startswith(keywords):
            return api_report_pb2.ReturnLint(returns_defined=True)
        # If the code only returns None then `Returns` section in the docstring is
        # not required.
        if all(return_val == 'None'
               for return_val in return_visitor.total_returns):
            return None
        return api_report_pb2.ReturnLint(returns_defined=False)

    return None
Esempio n. 2
0
def lint_returns(
        page_info: parser.PageInfo) -> Optional[api_report_pb2.ReturnLint]:
    """"Lints the returns block in the docstring.

  This linter only checks if a `Returns` block exists in the docstring
  if it finds `return` keyword in the source code.

  Args:
    page_info: A `PageInfo` object containing the information of a page
      generated via the api generation.

  Returns:
    A filled `ReturnLint` proto object.
  """
    source = _get_source(page_info.py_object)
    if source is not None and 'return' in source:
        for item in page_info.doc.docstring_parts:
            if isinstance(item, parser.TitleBlock):
                if item.title.lower().startswith('return'):
                    return api_report_pb2.ReturnLint(returns_defined=True)
        return api_report_pb2.ReturnLint(returns_defined=False)
    return None