Skip to content
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

Request for "with_attr_docs" feature #2

Open
hmeine opened this issue Sep 20, 2022 · 5 comments · Fixed by pydantic/pydantic#6563
Open

Request for "with_attr_docs" feature #2

hmeine opened this issue Sep 20, 2022 · 5 comments · Fixed by pydantic/pydantic#6563

Comments

@hmeine
Copy link

hmeine commented Sep 20, 2022

As mentioned in pydantic/pydantic#4492, I have been using https://github.com/danields761/pydantic-settings/ mostly for its with_attrs_docs decorator. (That being said, I wanted to link to its documentation here, but cannot find it…)

It greatly reduces the visual noise / clutter when documenting fields that otherwise do not even need a Field().

Hence, I would love to see this be supported in this new project as well (not caring so much about the actual syntax / import, in case the name should change).

@samuelcolvin
Copy link
Member

Well this is embarrassing, after looking into with_attrs_docs it seems like it would actually make more sense to implement this in pydantic itself or in a separate library (if it requires multiple extra dependencies), sorry for pointing you here 🤦.

Still, I'll leave the issue here for now and we can think more about the idea and how to implement it when we're nearer to V2 release.

More information on what this does, from here:


Extracts fields documentation

Allows to extract Sphinx style attributes documentation by processing AST tree of class definition

from pydantic import BaseModel
from pydantic_settings import with_attrs_docs


@with_attrs_docs
class Foo(BaseModel):
    bar: str
    """here is docs"""

    #: docs for baz
    baz: int

    #: yes
    #: of course
    is_there_multiline: bool = True


assert Foo.__fields__['bar'].field_info.description == 'here is docs'
assert Foo.__fields__['baz'].field_info.description == 'docs for baz'
assert Foo.__fields__['is_there_multiline'].field_info.description == (
    'yes\nof course'
)

@peak-stephen
Copy link

Would you accept a PR that adds a more limited form of this functionality? From looking into pydantic_settings (and the related class_doc) most of the code has to do with parsing comments. If you only support docstrings it's under 100 lines of code.

@samuelcolvin
Copy link
Member

Yes I think so, would love to review a PR.

@hmeine
Copy link
Author

hmeine commented Jan 25, 2023

So you mean only the bar format in the example above would be supported? I must say that I would have to get used to that format (which seems to be a strange de-facto standard in the ecosystem, but not officially supported by python itself), but I would still be happy to have that feature!

@alexmojaki
Copy link

This has now been implemented in pydantic/pydantic#6563. When it's released (not in 2.6.0, unfortunately) use_attribute_docstrings in the model config will activate the feature.

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 a pull request may close this issue.

4 participants