diff --git a/src/pytkdocs/loader.py b/src/pytkdocs/loader.py index 8482fc1..97ef6f0 100644 --- a/src/pytkdocs/loader.py +++ b/src/pytkdocs/loader.py @@ -449,6 +449,15 @@ def get_module_documentation(self, node: ObjectNode, select_members=None) -> Mod return root_object + @staticmethod + def _class_path(cls): + mod = cls.__module__ + qname = cls.__qualname__ + if mod == "builtins": + return qname + else: + return f"{mod}.{qname}" + def get_class_documentation(self, node: ObjectNode, select_members=None) -> Class: """ Get the documentation for a class and its children. @@ -462,7 +471,8 @@ def get_class_documentation(self, node: ObjectNode, select_members=None) -> Clas """ class_ = node.obj docstring = inspect.cleandoc(class_.__doc__ or "") - root_object = Class(name=node.name, path=node.dotted_path, file_path=node.file_path, docstring=docstring) + bases = [self._class_path(b) for b in class_.__bases__] + root_object = Class(name=node.name, path=node.dotted_path, file_path=node.file_path, docstring=docstring, bases=bases) # Even if we don't select members, we want to correctly parse the docstring attributes_data: Dict[str, Dict[str, Any]] = {} diff --git a/src/pytkdocs/objects.py b/src/pytkdocs/objects.py index d326ec4..03ba5f1 100644 --- a/src/pytkdocs/objects.py +++ b/src/pytkdocs/objects.py @@ -350,6 +350,18 @@ class Class(Object): possible_name_properties: List[ApplicableNameProperty] = [NAME_PRIVATE] + def __init__(self, *args, bases: List[str] = None, **kwargs): + """ + Initialize the object. + + Arguments: + *args: Arguments passed to the parent class Initialize the object. + bases: The base classes (dotted paths). + **kwargs: Arguments passed to the parent class Initialize the object. + """ + super().__init__(*args, **kwargs) + self.bases = bases or ["object"] + class Function(Object): """ diff --git a/src/pytkdocs/serializer.py b/src/pytkdocs/serializer.py index d779c39..81f2699 100644 --- a/src/pytkdocs/serializer.py +++ b/src/pytkdocs/serializer.py @@ -239,4 +239,6 @@ def serialize_object(obj: Object) -> dict: serialized["type"] = annotation_to_string(obj.type) # type: ignore if hasattr(obj, "signature"): # noqa: WPS421 (hasattr) serialized["signature"] = serialize_signature(obj.signature) # type: ignore + if hasattr(obj, "bases"): # noqa: WPS421 (hasattr) + serialized["bases"] = obj.bases # type: ignore return serialized diff --git a/tests/test_loader.py b/tests/test_loader.py index 55fd1b6..a0789e4 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -176,6 +176,7 @@ def test_loading_class(): loader = Loader() obj = loader.get_object_documentation("tests.fixtures.the_package.the_module.TheClass") assert obj.docstring == "The class docstring." + assert obj.bases == ["object"] def test_loading_class_with_multiline_docstring_starting_on_first_line():