Skip to content

Commit

Permalink
Fix error for model with no fields (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Jay Qi <[email protected]>
  • Loading branch information
jayqi and jayqi authored May 10, 2024
1 parent c687b03 commit 664a3f7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# erdantic Changelog

## v1.0.3 (2024-05-10)

- Fixed `StopIteration` error when rendering a model that has no fields. ([Issue #120](https://github.com/drivendataorg/erdantic/issues/120), [PR #121](https://github.com/drivendataorg/erdantic/pull/121))

## v1.0.2 (2024-04-11)

- Fixed `AttributeError` when adding a model that has a field annotated with certain typing special forms like `Any`, `Literal`, or `TypeVar` instances. ([Issue #114](https://github.com/drivendataorg/erdantic/issues/114), [PR #115](https://github.com/drivendataorg/erdantic/pull/115))
Expand Down
6 changes: 5 additions & 1 deletion erdantic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ def to_dot_label(self) -> str:
str: DOT language for table
"""
# Get number of columns dynamically from first row
num_cols = next(iter(self.fields.values())).to_dot_row().count("<td")
try:
num_cols = next(iter(self.fields.values())).to_dot_row().count("<td")
except StopIteration:
# No fields, so just use 1 column
num_cols = 1
# Concatenate DOT of all rows together
rows = "\n".join(field_info.to_dot_row() for field_info in self.fields.values()) + "\n"
return self._dot_table_template.format(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "erdantic"
version = "1.0.2"
version = "1.0.3"
description = "Entity relationship diagrams for Python data model classes like Pydantic."
readme = "README.md"
authors = [{ name = "DrivenData", email = "[email protected]" }, { name = "Jay Qi" }]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ class MyModel(pydantic.BaseModel):
diagram.add_model(MyModel)


def test_model_with_no_fields():
"""Model with no fields should not error."""

class EmptyModel(pydantic.BaseModel):
pass

diagram = EntityRelationshipDiagram()
diagram.add_model(EmptyModel)
diagram.to_dot()


def test_unsupported_forward_ref_resolution(monkeypatch):
"""Plugin implementation does not do anything to resolve forward references."""

Expand Down

0 comments on commit 664a3f7

Please sign in to comment.