Skip to content

griffe-pydantic¤

ci documentation pypi version gitter

Griffe extension for Pydantic.

Installation¤

pip install griffe-pydantic

Usage¤

Command-line¤

griffe dump mypackage -e griffe_pydantic

See command-line usage in Griffe's documentation.

Python¤

import griffe

griffe.load(
    "mypackage",
    extensions=griffe.load_extensions(
        [{"griffe_pydantic": {"schema": True}}]
    )
)

See programmatic usage in Griffe's documentation.

MkDocs¤

mkdocs.yml
plugins:
- mkdocstrings:
    handlers:
      python:
        options:
          extensions:
          - griffe_pydantic:
              schema: true

See MkDocs usage in Griffe's documentation.

Examples¤

from typing import Any
from pydantic import field_validator, model_validator, ConfigDict, BaseModel, Field


class ExampleModel(BaseModel):
    """An example model."""

    model_config = ConfigDict(frozen=False)

    field_without_default: str
    """Shows the *[Required]* marker in the signature."""

    field_plain_with_validator: int = 100
    """Show standard field with type annotation."""

    field_with_validator_and_alias: str = Field("FooBar", alias="BarFoo", validation_alias="BarFoo")
    """Shows corresponding validator with link/anchor."""

    field_with_constraints_and_description: int = Field(
        default=5, ge=0, le=100, description="Shows constraints within doc string."
    )

    @field_validator("field_with_validator_and_alias", "field_without_default", mode="before")
    @classmethod
    def check_max_length_ten(cls, v) -> str:
        """Show corresponding field with link/anchor."""
        if len(v) >= 10:
            raise ValueError("No more than 10 characters allowed")
        return v

    @model_validator(mode="before")
    @classmethod
    def lowercase_only(cls, data: dict[str, Any]) -> dict[str, Any]:
        """Ensure that the field without a default is lowercase."""
        if isinstance(data.get("field_without_default"), str):
            data["field_without_default"] = data["field_without_default"].lower()
        return data

ExampleModel ¤

Bases: BaseModel

An example model.

Methods:

Attributes:

field_plain_with_validator class-attribute instance-attribute ¤

field_plain_with_validator: int = 100

Show standard field with type annotation.

field_with_validator_and_alias class-attribute instance-attribute ¤

field_with_validator_and_alias: str = Field(
    "FooBar", alias="BarFoo", validation_alias="BarFoo"
)

Shows corresponding validator with link/anchor.

field_without_default instance-attribute ¤

field_without_default: str

Shows the [Required] marker in the signature.

check_max_length_ten classmethod ¤

check_max_length_ten(v) -> str

Show corresponding field with link/anchor.

lowercase_only classmethod ¤

lowercase_only(data: dict[str, Any]) -> dict[str, Any]

Ensure that the field without a default is lowercase.

ExampleModel pydantic-model ¤

Bases: BaseModel

An example model.

Config:

  • frozen: False

Fields:

Validators:

field_plain_with_validator pydantic-field ¤

field_plain_with_validator: int = 100

Show standard field with type annotation.

field_with_constraints_and_description pydantic-field ¤

field_with_constraints_and_description: int = 5

Shows constraints within doc string.

field_with_validator_and_alias pydantic-field ¤

field_with_validator_and_alias: str = 'FooBar'

Shows corresponding validator with link/anchor.

field_without_default pydantic-field ¤

field_without_default: str

Shows the [Required] marker in the signature.

check_max_length_ten pydantic-validator ¤

check_max_length_ten(v) -> str

Show corresponding field with link/anchor.

lowercase_only pydantic-validator ¤

lowercase_only(data: dict[str, Any]) -> dict[str, Any]

Ensure that the field without a default is lowercase.