Skip to content

Commit

Permalink
MAINT: Refactoring get_doc_object (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
F3eQnxN3RriK committed Mar 14, 2023
1 parent 9c59a46 commit 3d4a583
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
16 changes: 12 additions & 4 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,15 @@ def _is_show_member(self, name):
return True


def get_doc_object(obj, what=None, doc=None, config=None):
def get_doc_object(
obj,
what=None,
doc=None,
config=None,
class_doc=ClassDoc,
func_doc=FunctionDoc,
obj_doc=ObjDoc,
):
if what is None:
if inspect.isclass(obj):
what = "class"
Expand All @@ -741,10 +749,10 @@ def get_doc_object(obj, what=None, doc=None, config=None):
config = {}

if what == "class":
return ClassDoc(obj, func_doc=FunctionDoc, doc=doc, config=config)
return class_doc(obj, func_doc=func_doc, doc=doc, config=config)
elif what in ("function", "method"):
return FunctionDoc(obj, doc=doc, config=config)
return func_doc(obj, doc=doc, config=config)
else:
if doc is None:
doc = pydoc.getdoc(obj)
return ObjDoc(obj, doc, config=config)
return obj_doc(obj, doc, config=config)
30 changes: 11 additions & 19 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sphinx.jinja2glue import BuiltinTemplateLoader

from .docscrape import NumpyDocString, FunctionDoc, ClassDoc, ObjDoc
from .docscrape import get_doc_object as get_doc_object_orig
from .xref import make_xref


Expand Down Expand Up @@ -407,20 +408,10 @@ def __init__(self, obj, doc=None, config=None):
ObjDoc.__init__(self, obj, doc=doc, config=config)


# TODO: refactor to use docscrape.get_doc_object
def get_doc_object(obj, what=None, doc=None, config=None, builder=None):
if what is None:
if inspect.isclass(obj):
what = "class"
elif inspect.ismodule(obj):
what = "module"
elif isinstance(obj, Callable):
what = "function"
else:
what = "object"

if config is None:
config = {}

template_dirs = [os.path.join(os.path.dirname(__file__), "templates")]
if builder is not None:
template_loader = BuiltinTemplateLoader()
Expand All @@ -430,11 +421,12 @@ def get_doc_object(obj, what=None, doc=None, config=None, builder=None):
template_env = SandboxedEnvironment(loader=template_loader)
config["template"] = template_env.get_template("numpydoc_docstring.rst")

if what == "class":
return SphinxClassDoc(obj, func_doc=SphinxFunctionDoc, doc=doc, config=config)
elif what in ("function", "method"):
return SphinxFunctionDoc(obj, doc=doc, config=config)
else:
if doc is None:
doc = pydoc.getdoc(obj)
return SphinxObjDoc(obj, doc, config=config)
return get_doc_object_orig(
obj,
what=what,
doc=doc,
config=config,
class_doc=SphinxClassDoc,
func_doc=SphinxFunctionDoc,
obj_doc=SphinxObjDoc,
)

0 comments on commit 3d4a583

Please sign in to comment.