From 1d93e095e98e3e59f944ac7e98da246dd604adfa Mon Sep 17 00:00:00 2001 From: Jay Qi Date: Fri, 13 Sep 2024 23:50:30 -0400 Subject: [PATCH 1/2] Fix error from type annotations containing ellipsis --- HISTORY.md | 4 ++++ erdantic/core.py | 4 ++++ pyproject.toml | 2 +- tests/test_core.py | 14 +++++++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 4c4de68..a308d44 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ # erdantic Changelog +## v1.0.5 (Unreleased) + +- Fixed runtime `AttributeError` that occurred when creating a diagram that includes a model with a field that uses a type annotation with ellipsis literal (e.g., `tuple[int, ...]`). ([Issue #124](https://github.com/drivendataorg/erdantic/issues/124), [PR #125](https://github.com/drivendataorg/erdantic/pull/125)) + ## v1.0.4 (2024-07-16) - Fixed handling of `typing.Annotated` in cases where it's not the outermost generic type. ([Issue #122](https://github.com/drivendataorg/erdantic/issues/122), [PR #123](https://github.com/drivendataorg/erdantic/pull/123)) diff --git a/erdantic/core.py b/erdantic/core.py index cdc0939..84d9d4f 100644 --- a/erdantic/core.py +++ b/erdantic/core.py @@ -512,6 +512,10 @@ def _add_if_model(self, model: type, recurse: bool) -> bool: # These are not going to be models if "__qualname__" in str(e): return False + # ellipsis object (used for example in tuple[int, ...]) don't have __module__ attribute + # This is also not going to be a model + elif "__module__" in str(e): + return False raise if key not in self.models: try: diff --git a/pyproject.toml b/pyproject.toml index 2635cc7..859c5ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "erdantic" -version = "1.0.4" +version = "1.0.5" description = "Entity relationship diagrams for Python data model classes like Pydantic." readme = "README.md" authors = [{ name = "DrivenData", email = "info@drivendata.org" }, { name = "Jay Qi" }] diff --git a/tests/test_core.py b/tests/test_core.py index 25f6026..9504526 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -4,7 +4,7 @@ import os from pathlib import Path import sys -from typing import Any, AnyStr, List, Literal, Optional, TypeVar +from typing import Any, AnyStr, List, Literal, Optional, Tuple, TypeVar if sys.version_info >= (3, 9): from typing import Annotated @@ -200,6 +200,18 @@ class MyModel(pydantic.BaseModel): diagram = EntityRelationshipDiagram() diagram.add_model(MyModel) + diagram.to_dot() + + +def test_model_with_ellipsis(): + """Model with Ellipsis should not error.""" + + class EllipsisModel(pydantic.BaseModel): + ellipsis_field: Tuple[int, ...] + + diagram = EntityRelationshipDiagram() + diagram.add_model(EllipsisModel) + diagram.to_dot() def test_model_with_no_fields(): From 0b334a290a2e44644aa7635cba787eb6019b650a Mon Sep 17 00:00:00 2001 From: Jay Qi Date: Fri, 13 Sep 2024 23:52:01 -0400 Subject: [PATCH 2/2] Fix wrong number --- HISTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index a308d44..eb34f6e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,7 +2,7 @@ ## v1.0.5 (Unreleased) -- Fixed runtime `AttributeError` that occurred when creating a diagram that includes a model with a field that uses a type annotation with ellipsis literal (e.g., `tuple[int, ...]`). ([Issue #124](https://github.com/drivendataorg/erdantic/issues/124), [PR #125](https://github.com/drivendataorg/erdantic/pull/125)) +- Fixed runtime `AttributeError` that occurred when creating a diagram that includes a model with a field that uses a type annotation with ellipsis literal (e.g., `tuple[int, ...]`). ([Issue #124](https://github.com/drivendataorg/erdantic/issues/124), [PR #127](https://github.com/drivendataorg/erdantic/pull/127)) ## v1.0.4 (2024-07-16)