-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
try: | ||
default_value = self._member_objects["__pydantic_fields__"][ | ||
name | ||
].default | ||
except KeyError: | ||
default_value = obj |
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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:
- Add Python file to
test/testdata
, e.g.test/testdata/pydantic.py
- Register it here.
uv run test/test_snapshot.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
Generating snapshot tests appears to be blocked on the same issue as #792. |
Contributes to #793.
This PR implements quick-and-dirty support for Pydantic-style default values.
Open questions:
pdoc-pydantic
orpdoc[pydantic]
?