Skip to content

Automatic field description from docstrings for Pydantic dataclasses #11243

Closed
@emaheuxPEREN

Description

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Unlike BaseModel and TypedDict, description of fields from Pydantic dataclasses is NOT automatically populated from docstrings (despite using ConfigDict(use_attribute_docstrings=True))

Example Code

from typing_extensions import TypedDict

from pydantic import BaseModel, TypeAdapter, ConfigDict, with_config
from pydantic.dataclasses import dataclass

PYDANTIC_CFG = ConfigDict(use_attribute_docstrings=True)


class NativeModel(BaseModel):
    model_config = PYDANTIC_CFG

    x: int
    """my docstring"""


@with_config(PYDANTIC_CFG)
class TypedDictModel(TypedDict):
    x: int
    """my docstring"""


@dataclass(config=PYDANTIC_CFG)
class DataclassModel:
    x: int
    """my docstring"""


print(NativeModel.model_json_schema())
# {'properties': {'x': {'description': 'my docstring', 'title': 'X', 'type': 'integer'}}, 'required': ['x'], 'title': 'NativeModel', 'type': 'object'}

print(TypeAdapter(TypedDictModel).json_schema())
# {'properties': {'x': {'description': 'my docstring', 'title': 'X', 'type': 'integer'}}, 'required': ['x'], 'title': 'TypedDictModel', 'type': 'object'}

print(TypeAdapter(DataclassModel).json_schema())
# {'properties': {'x': {'title': 'X', 'type': 'integer'}}, 'required': ['x'], 'title': 'DataclassModel', 'type': 'object'}

Python, Pydantic & OS Version

pydantic version: 2.10.4
        pydantic-core version: 2.27.2
          pydantic-core build: profile=release pgo=false
               python version: 3.11.9 (main, May  2 2024, 10:11:35) [GCC 12.2.0]
                     platform: Linux-6.1.0-28-amd64-x86_64-with-glibc2.36
             related packages: typing_extensions-4.12.2

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions