Skip to content

Commit 3d4a583

Browse files
authored
MAINT: Refactoring get_doc_object (#452)
1 parent 9c59a46 commit 3d4a583

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

numpydoc/docscrape.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,15 @@ def _is_show_member(self, name):
727727
return True
728728

729729

730-
def get_doc_object(obj, what=None, doc=None, config=None):
730+
def get_doc_object(
731+
obj,
732+
what=None,
733+
doc=None,
734+
config=None,
735+
class_doc=ClassDoc,
736+
func_doc=FunctionDoc,
737+
obj_doc=ObjDoc,
738+
):
731739
if what is None:
732740
if inspect.isclass(obj):
733741
what = "class"
@@ -741,10 +749,10 @@ def get_doc_object(obj, what=None, doc=None, config=None):
741749
config = {}
742750

743751
if what == "class":
744-
return ClassDoc(obj, func_doc=FunctionDoc, doc=doc, config=config)
752+
return class_doc(obj, func_doc=func_doc, doc=doc, config=config)
745753
elif what in ("function", "method"):
746-
return FunctionDoc(obj, doc=doc, config=config)
754+
return func_doc(obj, doc=doc, config=config)
747755
else:
748756
if doc is None:
749757
doc = pydoc.getdoc(obj)
750-
return ObjDoc(obj, doc, config=config)
758+
return obj_doc(obj, doc, config=config)

numpydoc/docscrape_sphinx.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from sphinx.jinja2glue import BuiltinTemplateLoader
1212

1313
from .docscrape import NumpyDocString, FunctionDoc, ClassDoc, ObjDoc
14+
from .docscrape import get_doc_object as get_doc_object_orig
1415
from .xref import make_xref
1516

1617

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

409410

410-
# TODO: refactor to use docscrape.get_doc_object
411411
def get_doc_object(obj, what=None, doc=None, config=None, builder=None):
412-
if what is None:
413-
if inspect.isclass(obj):
414-
what = "class"
415-
elif inspect.ismodule(obj):
416-
what = "module"
417-
elif isinstance(obj, Callable):
418-
what = "function"
419-
else:
420-
what = "object"
421-
422412
if config is None:
423413
config = {}
414+
424415
template_dirs = [os.path.join(os.path.dirname(__file__), "templates")]
425416
if builder is not None:
426417
template_loader = BuiltinTemplateLoader()
@@ -430,11 +421,12 @@ def get_doc_object(obj, what=None, doc=None, config=None, builder=None):
430421
template_env = SandboxedEnvironment(loader=template_loader)
431422
config["template"] = template_env.get_template("numpydoc_docstring.rst")
432423

433-
if what == "class":
434-
return SphinxClassDoc(obj, func_doc=SphinxFunctionDoc, doc=doc, config=config)
435-
elif what in ("function", "method"):
436-
return SphinxFunctionDoc(obj, doc=doc, config=config)
437-
else:
438-
if doc is None:
439-
doc = pydoc.getdoc(obj)
440-
return SphinxObjDoc(obj, doc, config=config)
424+
return get_doc_object_orig(
425+
obj,
426+
what=what,
427+
doc=doc,
428+
config=config,
429+
class_doc=SphinxClassDoc,
430+
func_doc=SphinxFunctionDoc,
431+
obj_doc=SphinxObjDoc,
432+
)

0 commit comments

Comments
 (0)