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

Conversation

jinnovation
Copy link
Contributor

@jinnovation jinnovation commented May 1, 2025

Contributes to #793.

This PR implements quick-and-dirty support for Pydantic-style default values.

Open questions:

  • Should Pydantic support for pdoc be split out into a separate "plugin" package, e.g. pdoc-pydantic or pdoc[pydantic]?
  • Is this the best place for default-value-extraction logic for Pydantic classes to live?

Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Should Pydantic support for pdoc be split out into a separate "plugin" package, e.g. pdoc-pydantic or pdoc[pydantic]?

Too much complexity for now. I think pydantic is popular enough to have support for it builtin for now.

Is this the best place for default-value-extraction logic for Pydantic classes to live?

Pragmatically yes. See my comments below on how we can keep things reasonably clean.

pdoc/doc.py Outdated
Comment on lines 317 to 322
try:
default_value = self._member_objects["__pydantic_fields__"][
name
].default
except KeyError:
default_value = obj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this logic into pdoc/_pydantic.py and shorten it here to something like this:

Suggested change
try:
default_value = self._member_objects["__pydantic_fields__"][
name
].default
except KeyError:
default_value = obj
default_value = _pydantic.default_value(self, name, obj)

This allows us to keep all pydantic-specific logic in a central location if we ever decide to break it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

test/test_doc.py Outdated
cls: Class = m.members[member]

for k, v in defaults.items():
assert cls.members[k].default_value_str == v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a snapshot test instead:

  1. Add Python file to test/testdata, e.g. test/testdata/pydantic.py
  2. Register it here.
  3. uv run test/test_snapshot.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

@jinnovation jinnovation marked this pull request as draft May 2, 2025 17:23
@jinnovation
Copy link
Contributor Author

Generating snapshot tests appears to be blocked on the same issue as #792.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants