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
[Feature Request] Auto-Docstring generation for Schema bearing Models #638
Comments
I'm not opposed to it, my questions/feedback would be:
Before we add it, would anyone else want this? |
I think this would be pretty popular as it would interface with canonical Sphinx documentation tech. Effectively auto-docs from the Schema so that you do not need to write this twice. For speed, we could use the |
I'm quite in favor of this, as I've been beginning to create docs with pydoc-markdown for an API client I'm writing using Pydantic for data validation/parsing/coersion and such, and having to re-write my docstrings, especially for inherited models, is a bit tedious. I would gladly switch all my definitions to |
@dgasmith For what it's worth, you could implement it without modifying metaclass by making use of So ProtoModel would become: class ProtoModel(BaseModel):
def __init_subclass__(cls) -> None:
cls.__doc__ = AutoPydanticDocGenerator(cls, always_apply=True) and you could drop the metaclass. |
@dmontagu Thanks! I was not aware of this. |
@dmontagu 's comment was very helpful for finding a way to autogenerate my own documentation. I really like the |
Just a note, that environ-config library provides method |
Since this might be related, I created a sphinx autodoc extension called We use pydantic settings for many applications but lacked a proper way to document them appropriately within our sphinx documentation (e.g. sphinx autodoc does not show default values for fields, it is hard to distinguish validators from standard class methods etc...). It might be helpful for you, too. |
@mansenfranzen , autodoc_pydantic looks really great! Thanks for creating that sphinx autodoc extension! I found it the other day and have already started using it in my projects! It's also definitely related to this issue! There's one more thing, and I wonder if autodoc_pydantic can do it... One of the ideas presented in this issue was wanting to be able to set the class's Of course, there are pros and cons to setting a class's docstring, as discussed above. Moreover, If people are using tools like autodoc_pydantic, then who in their right mind would use Anyway, I was just curious what you think about it. Adding this capability (of setting a model's docstring) to pydantic is probably not wise. Offloading this functionality to tools like autodoc_pydantic seems much better, but now people must use Sphinx, and can't access their docstring via |
@nicobako I'm glad you're happy with autodoc_pydantic :-). To support the ipykernel's To provide the same functionality for all pydantic models regardless of whether they have the appropriate doc string or not, you'll need to overload/patch the Patching the |
@mansenfranzen sorry it has taken me a long time to respond to you. I agree with you that autodoc_pydantic shouldn't support this functionality. I also agree that this functionality, if it came into being, should not be part of pydantic. Even if it were a separate package, the truth is that it would be very difficult to standardize how a pydantic Model gets represented as a docstring... We would have endless debates about whether to use numpy-style, google-style, etc. I recently created a small blog site, and I just created an article to cover this topic. https://blog.nicobako.dev/articles/pydantic_autodoc.html My hope is that it will give people the tools they need to implement custom autodoc functionality themselves -- if they need to. |
What's the recommended way to get this feature in 2022? I am thinking about using pydantic to validate kwargs and generate document for it. Do I have to build this from scratch or are there some tools that could be used as a start point? |
Hey @link89 , I don't think there are any tools that will do this for you, but pydantic has a really nice API, and let's you introspect your models quite easily. I wrote up a blog post a while ago on how you could build this functionality yourself: https://blog.nicobako.dev/articles/pydantic_autodoc.html I think you'll find building your own solution to be easy, and enjoyable! |
See pydantic/pydantic-settings#2 for a related
discussion.
…On Mon, 28 Nov 2022, 19:55 Nico Bako, ***@***.***> wrote:
What's the recommended way to get this feature in 2022? I am thinking
about using pydantic to validate kwargs and generate document for it. Do I
have to build this from scratch or are there some tools that could be used
as a start point?
Hey @link89 <https://github.com/link89> , I don't think there are any
tools that will do this for you, but pydantic has a really nice API, and
let's you introspect your models quite easily.
I wrote up a blog post a while ago on how you could builds this
functionality yourself:
https://blog.nicobako.dev/articles/pydantic_autodoc.html
I think you'll find building your own solution to be easy, and enjoyable!
—
Reply to this email directly, view it on GitHub
<#638 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA62GGNTM2Z7MYWCJJQAT73WKUEZPANCNFSM4H5HSZ7A>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I'll just note here that for compatibility with v2, you'll probably need to change from using Because it seems that this issue has a good solution (namely, the docstring can be configured automatically in the |
When #6563 is released (not in 2.6.0, unfortunately) |
Feature Request
This request is to have Pydantic models auto-generate their doc strings for their parameters, reading the parameters'
Schema
objects for more information. The end result would be Model classes who's__doc__
provides details about the parameters the model has. This would have use for people who generate docs for their models though a program like Sphinx to auto create the more complete docstring.This can, and probably should be an optional thing the user sets or calls since it will require overwriting the
__doc__
variable.This may turn out to be too dependent on individual user preferences of doc style flavors to have any viable officially supported flavor(s) in pydantic, but I wanted to propose anyways.
Below I have a crude toy implementation with examples to show the outputs. I have tested this in Python 3.6 and 3.7 with Pydantic Versions 0.26 and 0.29 and should run as with no external dependencies beyond pydantic itself)
Foreseeable difficulties:
Schema
and not variablesKnown issues with toy implementation:
Schema
vs. non-Schema
parameters:class: TargetClass
instead of any further docstring description which would in Sphinx's RST format as a link to that class in the docs, not exactly helpful in all cases though)Outputs the following lines:
The text was updated successfully, but these errors were encountered: