def infer_arg(node, context=None): if not isinstance(node.parent, nodes.Arguments): raise UseInferenceDefault() if not isinstance(node.parent.parent, nodes.Function): raise UseInferenceDefault() func = node.parent.parent docstring = func.doc if docstring is None: raise UseInferenceDefault() doctree = etree.fromstring(publish_doctree(docstring).asdom().toxml()) field_lists = doctree.findall(".//field_list") fields = [f for field_list in field_lists for f in field_list.findall('field')] if fields: for field in fields: field_name = field.findall("field_name")[0].text field_body = field.findall("field_body")[0].findall("paragraph")[0].text if field_name == "type %s" % node.name: return parse_node(node, context, field_body) raise UseInferenceDefault()
def infer_rtype(node, context=None): if context is not None: context_copy = context.clone() else: context_copy = None for infer in node.func.infer(context_copy): if infer is YES: raise UseInferenceDefault() docstring = infer.doc if docstring is None: break doctree = etree.fromstring(publish_doctree(docstring).asdom().toxml()) field_lists = doctree.findall(".//field_list") fields = [f for field_list in field_lists for f in field_list.findall('field')] if fields: for field in fields: field_names = field.findall("field_name") field_bodies = field.findall("field_body") if not field_names: break if not field_bodies: break field_name = field_names[0].text paragraphs = field_bodies[0].findall("paragraph") if not paragraphs: break field_body = paragraphs[0].text if field_name.startswith("rtype"): return parse_node(node, context, field_body) # found nothing raise UseInferenceDefault()