Skip to content

Support defaults from Pydantic fields #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion pdoc/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
from pdoc.doc_types import resolve_annotations
from pdoc.doc_types import safe_eval_type

_PYDANTIC_ENABLED: bool

try: # pragma: no cover
import pydantic
except ImportError: # pragma: no cover
_PYDANTIC_ENABLED = False
finally: # pragma: no cover
_PYDANTIC_ENABLED = True


def _include_fullname_in_traceback(f):
"""
Expand Down Expand Up @@ -314,12 +323,27 @@ def members(self) -> dict[str, Doc]:
taken_from=taken_from,
)
else:
default_value = obj

if (
_PYDANTIC_ENABLED
and isinstance(self.obj, type)
and issubclass(self.obj, pydantic.BaseModel)
):
pydantic_fields = self.obj.__pydantic_fields__

default_value = (
pydantic_fields[name].default
if name in pydantic_fields
else obj
)

doc = Variable(
self.modulename,
qualname,
docstring="",
annotation=self._var_annotations.get(name, empty),
default_value=obj,
default_value=default_value,
taken_from=taken_from,
)
if self._var_docstrings.get(name):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dev-dependencies = [
"pytest-timeout>=2.3.1",
"hypothesis>=6.113.0",
"pdoc-pyo3-sample-library>=1.0.11",
"pydantic>=2.11.4",
]

[build-system]
Expand Down
1 change: 1 addition & 0 deletions test/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def outfile(self, format: str) -> Path:
"include_undocumented": False,
},
),
Snapshot("with_pydantic"),
]


Expand Down
54 changes: 54 additions & 0 deletions test/testdata/with_pydantic.html

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions test/testdata/with_pydantic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
A small example with Pydantic entities.
"""

import pydantic


class Foo(pydantic.BaseModel):
a: int = pydantic.Field(default=1, description="Docstring for a")

b: int = 2
"""Docstring for b."""
1 change: 1 addition & 0 deletions test/testdata/with_pydantic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<module with_pydantic # A small example with…>
149 changes: 148 additions & 1 deletion uv.lock

Large diffs are not rendered by default.

Loading