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

FieldInfo Description Docstrings are not working #9218

Closed
1 task done
Antyos opened this issue Apr 11, 2024 · 2 comments
Closed
1 task done

FieldInfo Description Docstrings are not working #9218

Antyos opened this issue Apr 11, 2024 · 2 comments
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@Antyos
Copy link

Antyos commented Apr 11, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I can't get the automatic docstring detection from #6563 to work using the example code.

Just to make sure, I created a new virtual environment and installed only Pydantic, and still cannot get it to work. Even with the use_attribute_docstrings=True, it still won't use the attribute docstrings.

I feel like I'm probably doing something wrong, but I'm stumped.

Example Code

from pydantic import BaseModel, ConfigDict, Field

class Model(BaseModel):
    model_config = ConfigDict(use_attribute_docstrings=True)
    x: str
    """
    Example of an attribute docstring
    """
    y: int = Field(description="Description in Field")
    """
    Description in Field overrides attribute docstring
    """
print(Model.model_fields["x"].description)
# None
print(Model.model_fields["y"].description)
# 'Description in Field'

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\avg170002\Code\temp\.venv\Lib\site-packages\pydantic
               python version: 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
                     platform: Windows-11-10.0.22621-SP0
             related packages: typing_extensions-4.11.0
                       commit: unknown
@Antyos Antyos added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 11, 2024
@Viicos
Copy link
Contributor

Viicos commented Apr 12, 2024

Are you running this in an interactive terminal? If so, this won't work as Pydantic needs to have access to the source code of the class, as stated in the docs.

@sydney-runkle
Copy link
Member

@Antyos,

I'm not able to reproduce this issue. Perhaps double check the version of pydantic that you're using?

from pydantic import BaseModel, ConfigDict, Field

class Model(BaseModel):
    model_config = ConfigDict(use_attribute_docstrings=True)
    x: str
    """
    Example of an attribute docstring
    """
    y: int = Field(description="Description in Field")
    """
    Description in Field overrides attribute docstring
    """
print(Model.model_fields["x"].description)
# Example of an attribute docstring
print(Model.model_fields["y"].description)
# 'Description in Field'

^^ using v2.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

3 participants