Skip to content

Commit f7f6652

Browse files
authored
feat: Include base classes in output
PR #108: #108 Issue mkdocstrings#269: mkdocstrings/mkdocstrings#269
1 parent 0133369 commit f7f6652

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/pytkdocs/loader.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,15 @@ def get_module_documentation(self, node: ObjectNode, select_members=None) -> Mod
449449

450450
return root_object
451451

452+
@staticmethod
453+
def _class_path(cls):
454+
mod = cls.__module__
455+
qname = cls.__qualname__
456+
if mod == "builtins":
457+
return qname
458+
else:
459+
return f"{mod}.{qname}"
460+
452461
def get_class_documentation(self, node: ObjectNode, select_members=None) -> Class:
453462
"""
454463
Get the documentation for a class and its children.
@@ -462,7 +471,8 @@ def get_class_documentation(self, node: ObjectNode, select_members=None) -> Clas
462471
"""
463472
class_ = node.obj
464473
docstring = inspect.cleandoc(class_.__doc__ or "")
465-
root_object = Class(name=node.name, path=node.dotted_path, file_path=node.file_path, docstring=docstring)
474+
bases = [self._class_path(b) for b in class_.__bases__]
475+
root_object = Class(name=node.name, path=node.dotted_path, file_path=node.file_path, docstring=docstring, bases=bases)
466476

467477
# Even if we don't select members, we want to correctly parse the docstring
468478
attributes_data: Dict[str, Dict[str, Any]] = {}

src/pytkdocs/objects.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ class Class(Object):
350350

351351
possible_name_properties: List[ApplicableNameProperty] = [NAME_PRIVATE]
352352

353+
def __init__(self, *args, bases: List[str] = None, **kwargs):
354+
"""
355+
Initialize the object.
356+
357+
Arguments:
358+
*args: Arguments passed to the parent class Initialize the object.
359+
bases: The base classes (dotted paths).
360+
**kwargs: Arguments passed to the parent class Initialize the object.
361+
"""
362+
super().__init__(*args, **kwargs)
363+
self.bases = bases or ["object"]
364+
353365

354366
class Function(Object):
355367
"""

src/pytkdocs/serializer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,6 @@ def serialize_object(obj: Object) -> dict:
239239
serialized["type"] = annotation_to_string(obj.type) # type: ignore
240240
if hasattr(obj, "signature"): # noqa: WPS421 (hasattr)
241241
serialized["signature"] = serialize_signature(obj.signature) # type: ignore
242+
if hasattr(obj, "bases"): # noqa: WPS421 (hasattr)
243+
serialized["bases"] = obj.bases # type: ignore
242244
return serialized

tests/test_loader.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def test_loading_class():
176176
loader = Loader()
177177
obj = loader.get_object_documentation("tests.fixtures.the_package.the_module.TheClass")
178178
assert obj.docstring == "The class docstring."
179+
assert obj.bases == ["object"]
179180

180181

181182
def test_loading_class_with_multiline_docstring_starting_on_first_line():

0 commit comments

Comments
 (0)