Skip to content
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

fix: Prevent crash in case of empty dataclasses #56

Merged
merged 2 commits into from
Jun 28, 2020
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
9 changes: 5 additions & 4 deletions src/pytkdocs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,15 @@ def get_class_documentation(self, node: ObjectNode, select_members=None) -> Clas
self.select_inherited_members and "__fields__" in all_members
):
root_object.properties = ["dataclass"]
for field_name, annotation in all_members["__annotations__"].items():
if self.select(field_name, select_members) and ( # type: ignore

for field in all_members["__dataclass_fields__"].values():
if self.select(field.name, select_members) and ( # type: ignore
self.select_inherited_members
# Same comment as for Pydantic models
or field_name
or field.name
not in chain(*(getattr(cls, "__dataclass_fields__", {}).keys() for cls in class_.__mro__[1:-1]))
):
child_node = ObjectNode(obj=annotation, name=field_name, parent=node)
child_node = ObjectNode(obj=field.type, name=field.name, parent=node)
root_object.add_child(self.get_annotated_dataclass_field(child_node))

return root_object
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ class Person:
name: str
age: int
"""Field description."""


@dataclass
class Empty:
"""A dataclass without any fields"""

pass
9 changes: 9 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ def test_loading_dataclass():
assert "dataclass" not in not_dataclass.properties


def test_loading_empty_dataclass():
"""Handle empty dataclasses."""
loader = Loader()
obj = loader.get_object_documentation("tests.fixtures.dataclass.Empty")
assert obj.docstring == "A dataclass without any fields"
assert len(obj.attributes) == 0
assert "dataclass" in obj.properties


def test_loading_pydantic_model():
"""Handle Pydantic models."""
loader = Loader()
Expand Down