-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
bug V2Bug related to Pydantic V2Bug related to Pydantic V2
Description
Initial Checks
- I confirm that I'm using Pydantic V2
Description
I'm writing a script to generate documentation for Pydantic classes. I'm using use_attribute_docstrings
and have noticed that the docstring description gets lost if the model uses a forward reference.
The code snippet below prints:
{'b': FieldInfo(annotation=ForwardRef('Model2'), required=True, description='doc1')}
{'b': FieldInfo(annotation=Model2, required=True)}
Note the missing description in the second line.
Swapping the order of Model1 and Model2 in the file to prevent the forward reference:
{'b': FieldInfo(annotation=Model2, required=True, description='doc1')}
{'b': FieldInfo(annotation=Model2, required=True, description='doc1')}
Looking at rebuild_model_fields()
, it seems to overwrite the existing FieldInfo
s, which causes the existing description (that was obtained from the docstring) to be discarded. Would be appropriate to copy any existing description to the rebuild_fields
in this method?
Example Code
from __future__ import annotations
from pydantic import BaseModel
class Model1(BaseModel, use_attribute_docstrings=True):
b: Model2
"""doc1"""
class Model2(BaseModel, use_attribute_docstrings=True):
pass
print(Model1.model_fields)
Model1.model_rebuild()
print(Model1.model_fields)
Python, Pydantic & OS Version
pydantic version: 2.11.1
pydantic-core version: 2.33.0
pydantic-core build: profile=release pgo=false
install path: /Users/anvilpete/projects/myproject/.venv/lib/python3.12/site-packages/pydantic
python version: 3.12.6 (main, Sep 9 2024, 21:36:32) [Clang 18.1.8 ]
platform: macOS-15.3.2-arm64-arm-64bit
related packages: typing_extensions-4.12.2
commit: unknown
Metadata
Metadata
Assignees
Labels
bug V2Bug related to Pydantic V2Bug related to Pydantic V2