def autodoc(t: Type): try: docstr = type_docs.DOCS[t] except KeyError: raise NotImplementedError(t) try: doc_tree = parse(docstr) except LarkError as e: raise AutoDocError(f"Error in docstring of type {t}") assert {s.name for s in doc_tree.sections} <= {'Example', 'Examples', 'Note', 'See Also'}, [s.name for s in doc_tree.sections] if t.proto_attrs: methods_doc = Section('Methods', [doc_func(f, t) for f in t.proto_attrs.values() if isinstance(f, Function)]) doc_tree.sections.insert(0, methods_doc) if t in subtypes: subtypes_doc = Section('Subtypes', [Text([str(st) + ", "]) for st in subtypes[t]]) doc_tree.sections.insert(0, subtypes_doc) if t.supertypes: supertypes_doc = Section('Supertypes', [Text([str(st)]) for st in t.supertypes]) doc_tree.sections.insert(0, supertypes_doc) return TypeDoc(t, doc_tree)
def doc_func(f, parent_type=None): if isinstance(f, MethodInstance): f = f.func try: doc_tree = parse(f.docstring or '') except LarkError as e: raise AutoDocError(f"Error in docstring of function {f.name}: {e}") assert {s.name for s in doc_tree.sections} <= {'Parameters', 'Example', 'Examples', 'Note', 'Returns', 'See Also'}, [s.name for s in doc_tree.sections] try: params_doc = doc_tree.get_section('Parameters') except KeyError: if f.params: params_doc = Section('Parameters', [Defin(p.name, None, str(p.type) if p.type else '') for p in f.params]) doc_tree.sections.insert(0, params_doc) else: params = list(f.params) if f.param_collector: params.append(f.param_collector) if len(params) != len(params_doc.items): raise AutoDocError(f"Parameters don't match docstring in function {f}") for d, p in safezip(params_doc.items, params): assert d.name == p.name, (d.name, p.name) d.type = str(p.type) if p.type else '' d.default = p.default.repr() if p.default else '' return FuncDoc(f, doc_tree, parent_type=parent_type)