Skip to content

MAINT: Refactoring get_doc_object #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)