Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Preserve field description when rebuilding model fields#11698

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Viicos merged 1 commit intomainfromrebuild-fields-desc
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletionspydantic/_internal/_fields.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -319,18 +319,20 @@ def rebuild_model_fields(
if field_info._complete:
rebuilt_fields[f_name] = field_info
else:
existing_desc = field_info.description
ann = _typing_extra.eval_type(
field_info._original_annotation,
*ns_resolver.types_namespace,
)
ann = _generics.replace_types(ann, typevars_map)

if (assign := field_info._original_assignment) is PydanticUndefined:
rebuilt_fields[f_name] = FieldInfo_.from_annotation(ann, _source=AnnotationSource.CLASS)
new_field = FieldInfo_.from_annotation(ann, _source=AnnotationSource.CLASS)
else:
rebuilt_fields[f_name] = FieldInfo_.from_annotated_attribute(
ann, assign, _source=AnnotationSource.CLASS
)
new_field = FieldInfo_.from_annotated_attribute(ann, assign, _source=AnnotationSource.CLASS)
# The description might come from the docstring if `use_attribute_docstrings` was `True`:
new_field.description = new_field.description if new_field.description is not None else existing_desc
rebuilt_fields[f_name] = new_field

return rebuilt_fields

Expand Down
18 changes: 18 additions & 0 deletionstests/test_fields.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -170,3 +170,21 @@ class Model(BaseModel):
field: str = Field(coerce_numbers_to_str=True)

assert Model(field=number).field == str(number)


def test_rebuild_model_fields_preserves_description() -> None:
"""https://github.com/pydantic/pydantic/issues/11696"""

class Model(BaseModel):
model_config = ConfigDict(use_attribute_docstrings=True)

f: 'Int'
"""test doc"""

assert Model.model_fields['f'].description == 'test doc'

Int = int

Model.model_rebuild()

assert Model.model_fields['f'].description == 'test doc'
Loading

[8]ページ先頭

©2009-2025 Movatter.jp