griffe-pydantic¤
Griffe extension for Pydantic.
Installation¤
This project is available to sponsors only, through my Insiders program. See Insiders explanation and installation instructions.
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¤
plugins:
- mkdocstrings:
handlers:
python:
options:
extensions:
- griffe_pydantic:
schema: true
See MkDocs usage in Griffe's documentation.
Examples¤
from pydantic import field_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")
@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
ExampleModel ¤
Bases: BaseModel
An example model.
Methods:
-
check_max_length_ten
–Show corresponding field with link/anchor.
Attributes:
-
field_plain_with_validator
(int
) –Show standard field with type annotation.
-
field_with_validator_and_alias
(str
) –Shows corresponding validator with link/anchor.
-
field_without_default
(str
) –Shows the [Required] marker in the signature.
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.
ExampleModel pydantic-model
¤
Bases: BaseModel
An example model.
Config:
frozen
:False
Fields:
-
field_without_default
(str
) -
field_plain_with_validator
(int
) -
field_with_validator_and_alias
(str
) -
field_with_constraints_and_description
(int
)
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.