Skip to content

Include base classes in output #108

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 1 commit into from
Jul 1, 2021
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
12 changes: 11 additions & 1 deletion src/pytkdocs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]] = {}
Expand Down
12 changes: 12 additions & 0 deletions src/pytkdocs/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 2 additions & 0 deletions src/pytkdocs/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down