Skip to content

erdantic.dataclasses

Classes

DataClassField

Concrete field adapter class for dataclass fields.

Attributes:

Name Type Description
field dataclasses.Field

The dataclass field instance that is associated with this adapter instance

Attributes

name: str property readonly

Name of this field on the parent data model.

type_name: str inherited 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: Field) special
Source code in erdantic/dataclasses.py
def __init__(self, field: dataclasses.Field):
    if not isinstance(field, dataclasses.Field):
        raise ValueError(f"field must be of type dataclasses.Field. Got: {type(field)}")
    super().__init__(field=field)
dot_row(self) -> str inherited

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/dataclasses.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/dataclasses.py
def is_many(self) -> bool:
    origin = get_origin(self.type_obj)
    return isinstance(origin, type) and (
        issubclass(origin, collections.abc.Container)
        or issubclass(origin, collections.abc.Iterable)
        or issubclass(origin, collections.abc.Sized)
    )
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/dataclasses.py
def is_nullable(self) -> bool:
    return get_origin(self.type_obj) is Union and type(None) in get_args(self.type_obj)

DataClassModel

Concrete model adapter class for a dataclasses module dataclass.

Attributes:

Name Type Description
model type

The dataclass that is associated with this adapter instance

Attributes

docstring: str inherited property readonly

Docstring for this data model.

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

List of fields defined on this data model.

key: str inherited property readonly

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

name: str inherited property readonly

Name of this data model.

Methods

__init__(self, model: type) special
Source code in erdantic/dataclasses.py
def __init__(self, model: type):
    if not self.is_model_type(model):
        raise ValueError(f"Argument model must be a dataclass: {repr(model)}")
    super().__init__(model=model)
dot_label(self) -> str inherited

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/dataclasses.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/dataclasses.py
@staticmethod
def is_model_type(obj: Any) -> bool:
    return isinstance(obj, type) and dataclasses.is_dataclass(obj)
Back to top