Skip to content

Add rendering of pydantic-based modelsΒ #833

@mhoeijm

Description

@mhoeijm

πŸ“ Description of the feature

Autoapi does not retrieve information when we use pydantics Field methods inside a pydantic DataModel class.

For instance, the following example:

from pydantic import BaseModel, Field

class ExamplePydanticClass(BaseModel):
    """An example Pydantic class.

    Parameters
    ----------
    value : float
        A required parameter.
    value_with_default1 : float, optional
        A simple optional parameter with a default value of 3.14.
    value_with_default2 : float, optional
        A simple optional parameter using Field for the default value and description.
        Default is 3.14.
    value_with_constraints : float, optional
        An optional parameter with constraints using Field for the default value,
        description and constraints. Default is 10.0, with constraints: 0.0 <= value <= 100.0.

    Examples
    --------
    >>> example = ExamplePydanticClass(value=42)
    >>> example.value
    42
    >>> example.value_with_default1
    3.14
    >>> example.value_with_default2
    3.14
    >>> example.value_with_constraints
    10.0

    """

    value: float
    """A required parameter."""

    value_with_default1: float = 3.14
    """A simple optional parameter with a default value."""

    value_with_default2: float = Field(
        default=3.14,
        description="A simple optional parameter using field for default value and description.",
    )

    value_with_constraints: float = Field(
        default=10.0,
        description="A optional parameter with constraints using field for default value, "
        "description and constraints.",
        le=100.0,
        ge=0.0,
    )

Is currently rendered as:

Image

Note that the informative parameters (default, description, le, ge, etc) specified in the Field method are not picked up in the rendering of the html page. It would be helpful if this information is somehow picked up by autoapi so that the generated docs are as complete as possible for such classes.

There is a drop-in solution for autodoc (autodoc_pydantic) that seems to do a good job at this. But there does not seem to be an equivalent for autoapi.

πŸ’‘ Steps for implementing the feature

No response

πŸ”— Useful links and references

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementGeneral improvements to existing features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions