Skip to content

erdantic.dataclasses

Classes

DataClassField (Field)

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.

Source code in erdantic/dataclasses.py
class DataClassField(Field[dataclasses.Field]):
    """Concrete field adapter class for dataclass fields.

    Attributes:
        field (dataclasses.Field): The dataclass field instance that is associated with this
            adapter instance.
    """

    def __init__(self, field: dataclasses.Field):
        if not isinstance(field, dataclasses.Field):
            raise InvalidFieldError(f"field must be of type dataclasses.Field. Got: {type(field)}")
        super().__init__(field=field)

    @property
    def name(self) -> str:
        return self.field.name

    @property
    def type_obj(self) -> Union[type, GenericAlias]:
        return self.field.type

    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)
        )

    def is_nullable(self) -> bool:
        return get_origin(self.type_obj) is Union and type(None) in get_args(self.type_obj)

Attributes

name: str property readonly

Name of this field on the parent data model.

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 InvalidFieldError(f"field must be of type dataclasses.Field. Got: {type(field)}")
    super().__init__(field=field)
is_many(self) -> bool

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

Returns:

Type Description
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

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 (Model)

Concrete model adapter class for a dataclasses module dataclass.

Attributes:

Name Type Description
model type

The dataclass that is associated with this adapter instance.

forward_ref_help Optional[str]

Instructions for how to resolve an unevaluated forward reference in a field's type declaration.

Source code in erdantic/dataclasses.py
@register_model_adapter("dataclasses")
class DataClassModel(Model[type]):
    """Concrete model adapter class for a
    [`dataclasses` module](https://docs.python.org/3/library/dataclasses.html) dataclass.

    Attributes:
        model (type): The dataclass that is associated with this adapter instance.
        forward_ref_help (Optional[str]): Instructions for how to resolve an unevaluated forward
            reference in a field's type declaration.
    """

    forward_ref_help = (
        "Call 'typing.get_type_hints' on your dataclass after creating it to resolve."
    )

    def __init__(self, model: type):
        if not self.is_model_type(model):
            raise InvalidModelError(f"Argument model must be a dataclass: {repr(model)}")
        super().__init__(model=model)

    @staticmethod
    def is_model_type(obj: Any) -> bool:
        return isinstance(obj, type) and dataclasses.is_dataclass(obj)

    @property
    def fields(self) -> List[Field]:
        return [DataClassField(field=f) for f in dataclasses.fields(self.model)]

Attributes

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

List of fields defined on this data model.

forward_ref_help: Optional[str]

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 InvalidModelError(f"Argument model must be a dataclass: {repr(model)}")
    super().__init__(model=model)
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)