Skip to content

erdantic.plugins.pydantic

get_fields_from_pydantic_model

get_fields_from_pydantic_model(
    model: PydanticModel,
) -> List[FieldInfo]

Given a Pydantic model, return a list of FieldInfo instances for each field in the model.

Parameters:

Name Type Description Default
model PydanticModel

The Pydantic model to get fields from.

required

Returns:

Type Description
List[FieldInfo]

List of FieldInfo instances for each field in the model

Source code in erdantic/plugins/pydantic.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def get_fields_from_pydantic_model(model: PydanticModel) -> List[FieldInfo]:
    """Given a Pydantic model, return a list of FieldInfo instances for each field in the model.

    Args:
        model (PydanticModel): The Pydantic model to get fields from.

    Returns:
        List[FieldInfo]: List of FieldInfo instances for each field in the model
    """
    try:
        # Rebuild model schema to resolve forward references
        model.model_rebuild()
    except pydantic.errors.PydanticUndefinedAnnotation as e:
        model_full_name = FullyQualifiedName.from_object(model)
        forward_ref = e.name
        msg = (
            f"Failed to resolve forward reference '{forward_ref}' in the type annotations for "
            f"Pydantic model {model_full_name}. "
            "You should use the model's model_rebuild() method to manually resolve it."
        )
        raise UnresolvableForwardRefError(
            msg, name=forward_ref, model_full_name=model_full_name
        ) from e
    return [
        FieldInfo.from_raw_type(
            model_full_name=FullyQualifiedName.from_object(model),
            name=name,
            # typing special forms currently get typed as object
            # https://github.com/python/mypy/issues/9773
            raw_type=pydantic_field_info.annotation or Any,  # type: ignore
        )
        for name, pydantic_field_info in model.model_fields.items()
    ]

get_fields_from_pydantic_v1_model

get_fields_from_pydantic_v1_model(
    model: PydanticV1Model,
) -> List[FieldInfo]

Given a Pydantic V1 model, return a list of FieldInfo instances for each field in the model.

Parameters:

Name Type Description Default
model PydanticV1Model

The Pydantic V1 model to get fields from.

required

Returns:

Type Description
List[FieldInfo]

List of FieldInfo instances for each field in the model

Source code in erdantic/plugins/pydantic.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def get_fields_from_pydantic_v1_model(model: PydanticV1Model) -> List[FieldInfo]:
    """Given a Pydantic V1 model, return a list of FieldInfo instances for each field in the model.

    Args:
        model (PydanticV1Model): The Pydantic V1 model to get fields from.

    Returns:
        List[FieldInfo]: List of FieldInfo instances for each field in the model
    """
    try:
        model.update_forward_refs()
    except NameError as e:
        model_full_name = FullyQualifiedName.from_object(model)
        # NameError attribute 'name' was added in Python 3.10
        forward_ref = getattr(
            e,
            "name",
            re.search(r"(?<=')(?:[^'])*(?=')", str(e)).group(0),  # type: ignore [union-attr]
        )
        msg = (
            f"Failed to resolve forward reference '{forward_ref}' in the type annotations for "
            f"Pydantic V1 model {model_full_name}. "
            "You should call the method update_forward_refs(**locals()) on the model in "
            "the scope where it has been defined to manually resolve it."
        )
        raise UnresolvableForwardRefError(
            msg, name=forward_ref, model_full_name=model_full_name
        ) from e

    return [
        FieldInfo.from_raw_type(
            model_full_name=FullyQualifiedName.from_object(model),
            name=name,
            raw_type=get_type_annotation_from_pydantic_v1_field(field),
        )
        for name, field in model.__fields__.items()
    ]

get_type_annotation_from_pydantic_v1_field

get_type_annotation_from_pydantic_v1_field(
    field_info: ModelField,
) -> type

Utility function to get the type annotation from a Pydantic V1 field info object.

Source code in erdantic/plugins/pydantic.py
130
131
132
133
134
135
def get_type_annotation_from_pydantic_v1_field(field_info: pydantic.v1.fields.ModelField) -> type:
    """Utility function to get the type annotation from a Pydantic V1 field info object."""
    tp = field_info.outer_type_
    if field_info.allow_none:
        return Optional[tp]  # type: ignore
    return tp

is_pydantic_model

is_pydantic_model(obj: Any) -> TypeGuard[PydanticModel]

Predicate function to determine if an object is a Pydantic model (not an instance).

Parameters:

Name Type Description Default
obj Any

The object to check.

required

Returns:

Type Description
bool

True if the object is a Pydantic model, False otherwise.

Source code in erdantic/plugins/pydantic.py
22
23
24
25
26
27
28
29
30
31
def is_pydantic_model(obj: Any) -> TypeGuard[PydanticModel]:
    """Predicate function to determine if an object is a Pydantic model (not an instance).

    Args:
        obj (Any): The object to check.

    Returns:
        bool: True if the object is a Pydantic model, False otherwise.
    """
    return isinstance(obj, type) and issubclass(obj, pydantic.BaseModel)

is_pydantic_v1_model

is_pydantic_v1_model(obj) -> TypeGuard[PydanticV1Model]

Predicate function to determine if an object is a Pydantic V1 model (not an instance). This is for models that use the legacy pydantic.v1 namespace.

Parameters:

Name Type Description Default
obj Any

The object to check.

required

Returns:

Type Description
bool

True if the object is a Pydantic V1 model, False otherwise.

Source code in erdantic/plugins/pydantic.py
78
79
80
81
82
83
84
85
86
87
88
def is_pydantic_v1_model(obj) -> TypeGuard[PydanticV1Model]:
    """Predicate function to determine if an object is a Pydantic V1 model (not an instance). This
    is for models that use the legacy `pydantic.v1` namespace.

    Args:
        obj (Any): The object to check.

    Returns:
        bool: True if the object is a Pydantic V1 model, False otherwise.
    """
    return isinstance(obj, type) and issubclass(obj, pydantic.v1.BaseModel)