Skip to content

erdantic.base

Attributes

FT

Type variable for a field object adapted by adapter class Field.

model_adapter_registry: Dict[str, Type[erdantic.base.Model]]

Registry of concrete Model adapter subclasses. A concrete Model subclass must be registered for it to be available to the diagram creation workflow.

MT

Type variable for a data model class adapted by adapter class Model. Bounded by type.

Classes

Field

Abstract base class that adapts a field object of a data model class to work with erdantic. Concrete implementations should subclass and implement abstract methods.

Attributes:

Name Type Description
field FT

Field object on a data model class associated with this adapter

Attributes

name: str property readonly

Name of this field on the parent data model.

type_name: str property readonly

String representation of the Python type annotation for this field.

type_obj: Union[type, _GenericAlias] property readonly

Python type object for this field.

Methods

__init__(self, field: +FT) special

Initialize Field adapter instance.

Parameters:

Name Type Description Default
field +FT

Field object to associate with this adapter instance

required
Source code in erdantic/base.py
@abstractmethod
def __init__(self, field: FT):
    """Initialize Field adapter instance.

    Args:
        field: Field object to associate with this adapter instance
    """
    self.field: Final[FT] = field
dot_row(self) -> str

Returns the DOT language "HTML-like" syntax specification of a row detailing this field that is part of a table describing the field's parent data model. It is used as part the label attribute of data model's node in the graph's DOT representation.

Returns:

Type Description
str

str: DOT language for table row

Source code in erdantic/base.py
def dot_row(self) -> str:
    """Returns the DOT language "HTML-like" syntax specification of a row detailing this field
    that is part of a table describing the field's parent data model. It is used as part the
    `label` attribute of data model's node in the graph's DOT representation.

    Returns:
        str: DOT language for table row
    """
    return _row_template.format(name=self.name, type_name=self.type_name)
is_many(self) -> bool

Check whether this field represents a one-to-one or one-to-many relationship.

Returns:

Type Description
bool

bool: True if one-to-many relationship, else False.

Source code in erdantic/base.py
@abstractmethod
def is_many(self) -> bool:  # pragma: no cover
    """Check whether this field represents a one-to-one or one-to-many relationship.

    Returns:
        bool: True if one-to-many relationship, else False.
    """
    pass
is_nullable(self) -> bool

Check whether this field is nullable, i.e., can be None.

Returns:

Type Description
bool

bool: True if nullable, else False.

Source code in erdantic/base.py
@abstractmethod
def is_nullable(self) -> bool:  # pragma: no cover
    """Check whether this field is nullable, i.e., can be `None`.

    Returns:
        bool: True if nullable, else False.
    """
    pass

Model

Abstract base class that adapts a data model class to work with erdantic. Instances represent a node in our entity relationship diagram graph. Concrete implementations should subclass and implement abstract methods.

Attributes:

Name Type Description
model MT

Data model class associated with this adapter

Attributes

docstring: str property readonly

Docstring for this data model.

fields: List[erdantic.base.Field] property readonly

List of fields defined on this data model.

key: str property readonly

Human-readable unique identifier for this data model. Should be stable across sessions.

name: str property readonly

Name of this data model.

Methods

__init__(self, model: +MT) special

Initialize model adapter instance.

Parameters:

Name Type Description Default
model +MT

Data model class to associate with this adapter instance

required
Source code in erdantic/base.py
@abstractmethod
def __init__(self, model: MT):
    """Initialize model adapter instance.

    Args:
        model: Data model class to associate with this adapter instance
    """
    self.model: Final[MT] = model
dot_label(self) -> str

Returns the DOT language "HTML-like" syntax specification of a table for this data model. It is used as the label attribute of data model's node in the graph's DOT representation.

Returns:

Type Description
str

str: DOT language for table

Source code in erdantic/base.py
def dot_label(self) -> str:
    """Returns the DOT language "HTML-like" syntax specification of a table for this data
    model. It is used as the `label` attribute of data model's node in the graph's DOT
    representation.

    Returns:
        str: DOT language for table
    """
    rows = "\n".join(field.dot_row() for field in self.fields)
    return _table_template.format(name=self.name, rows=rows).replace("\n", "")
is_model_type(obj: Any) -> bool staticmethod

Check if object is the type of data model class that this model adapter works with.

Source code in erdantic/base.py
@staticmethod
@abstractmethod
def is_model_type(obj: Any) -> bool:  # pragma: no cover
    """Check if object is the type of data model class that this model adapter works with."""
    pass

Functions

register_model_adapter(type_name: str) -> Callable[[Type[erdantic.base.Model]], Type[erdantic.base.Model]]

Create decorator to register a concrete Model adapter subclass that will be identified under the key type_name. A concrete Model subclass must be registered for it to be available to the diagram creation workflow.

Parameters:

Name Type Description Default
type_name str

Key used to identify concrete Model adapter subclass

required

Returns:

Type Description
Callable[[Type[erdantic.base.Model]], Type[erdantic.base.Model]]

Callable[[Type[Model]], Type[Model]]: A registration decorator for a concrete Model adapter subclass

Source code in erdantic/base.py
def register_model_adapter(type_name: str) -> Callable[[Type[Model]], Type[Model]]:
    """Create decorator to register a concrete [`Model`][erdantic.base.Model] adapter subclass
    that will be identified under the key `type_name`. A concrete `Model` subclass must be
    registered for it to be available to the diagram creation workflow.

    Args:
        type_name (str): Key used to identify concrete `Model` adapter subclass

    Returns:
        Callable[[Type[Model]], Type[Model]]: A registration decorator for a concrete `Model`
            adapter subclass
    """

    def decorator(cls: type) -> type:
        global model_adapter_registry
        if not issubclass(cls, Model):
            raise ValueError("Only subclasses of Model can be registered.")
        model_adapter_registry[type_name] = cls
        return cls

    return decorator
Back to top