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 improper handling of Annotated when not outermost generic type #123

Merged
merged 2 commits into from
Jul 16, 2024
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
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.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))

## 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))
Expand Down
16 changes: 2 additions & 14 deletions erdantic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@
import os
import sys
import textwrap
from typing import Any, Dict, Generic, Mapping, Optional, Type, TypeVar, Union

if sys.version_info >= (3, 9):
from typing import Annotated, get_args, get_origin
else:
# Annotated added in Python 3.9. typing's get_args and get_origin behave weirdly with
# so need to use typing_extensions version
from typing_extensions import Annotated, get_args, get_origin

from typing import Any, Dict, Generic, Mapping, Optional, Type, TypeVar, Union, get_args

if sys.version_info >= (3, 11):
from typing import Self
Expand Down Expand Up @@ -142,11 +134,7 @@ def from_raw_type(cls, model_full_name: FullyQualifiedName, name: str, raw_type:
Returns:
Self: _description_
"""
if get_origin(raw_type) is Annotated:
# Drop the Annotated extra metadata for the string representation
type_name = typenames(get_args(raw_type)[0], remove_modules=REMOVE_ALL_MODULES)
else:
type_name = typenames(raw_type, remove_modules=REMOVE_ALL_MODULES)
type_name = typenames(raw_type, remove_modules=REMOVE_ALL_MODULES)
field_info = cls(
model_full_name=model_full_name,
name=name,
Expand Down
4 changes: 2 additions & 2 deletions 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.3"
version = "1.0.4"
description = "Entity relationship diagrams for Python data model classes like Pydantic."
readme = "README.md"
authors = [{ name = "DrivenData", email = "[email protected]" }, { name = "Jay Qi" }]
Expand All @@ -30,7 +30,7 @@ dependencies = [
"pydantic-core",
"pygraphviz",
"sortedcontainers-pydantic",
"typenames",
"typenames >= 1.3",
"typer",
"typing_extensions>4 ; python_version < '3.12'",
]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ class DummyModel: ...
assert field_info.type_name == "str"
assert field_info.raw_type == tp

tp = Annotated[str, object()]
field_info = FieldInfo.from_raw_type(
model_full_name=FullyQualifiedName.from_object(DummyModel), name="dummy", raw_type=tp
)
assert field_info.type_name == "str"
assert field_info.raw_type == tp

tp = Optional[Annotated[str, object()]]
field_info = FieldInfo.from_raw_type(
model_full_name=FullyQualifiedName.from_object(DummyModel), name="dummy", raw_type=tp
)
assert field_info.type_name == "Optional[str]"
assert field_info.raw_type == tp


def test_field_not_found_error():
"""A FieldInfo for a model field that does not exist should error if trying to get the raw
Expand Down
Loading