drivendataorg / erdantic

Entity relationship diagrams for Python data model classes like Pydantic
https://erdantic.drivendata.org/
MIT License
317 stars 20 forks source link

Support edges based on TypeVar constraints/bounds #117

Open jayqi opened 5 months ago

jayqi commented 5 months ago

typing.TypeVar are used for annotating generics, and they can take constraints or bounds.

It probably makes sense to consider those to be an edge. TypeVar annotations are currently not considered a model. To support this, we'll need to special case TypeVar instances and introspect them to get this information.

Example of current behavior:

from typing import Generic, TypeVar, Optional, List

import erdantic as erd
from pydantic import BaseModel, field_validator
import rich

class DataModel(BaseModel):
    """Payload representation."""
    numbers: List[int]
    people: List[str]

DataT = TypeVar('DataT', DataModel, str)

class Response(BaseModel, Generic[DataT]):
    """HTTP Response representation."""

    data: Optional[DataT]

diagram = erd.create(Response)
rich.print(diagram)
#> EntityRelationshipDiagram(models={'__main__.Response': ModelInfo(...)}, edges={})

Desired:

An edge from Response to DataModel should be detected.