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

Commitbcd10f7

Browse files
authored
Always store the original field assignment onFieldInfo (#11946)
1 parent43bbdc0 commitbcd10f7

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

‎pydantic/_internal/_fields.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,13 @@ def collect_model_fields( # noqa: C901
334334
)
335335

336336
field_info=FieldInfo_.from_annotated_attribute(ann_type,assigned_value,_source=AnnotationSource.CLASS)
337+
# Store the original annotation and assignment value that should be used to rebuild the field info later.
338+
# Note that the assignment is always stored as the annotation might contain a type var that is later
339+
# parameterized with an unknown forward reference (and we'll need it to rebuild the field info):
340+
field_info._original_assignment=original_assignment
337341
ifnotevaluated:
338342
field_info._complete=False
339-
# Store the original annotation and assignment value that should be used to rebuild
340-
# the field info later:
341343
field_info._original_annotation=ann_type
342-
field_info._original_assignment=original_assignment
343344
elif'final'infield_info._qualifiersandnotfield_info.is_required():
344345
warnings.warn(
345346
f'Annotation{ann_name!r} is marked as final and has a default value. Pydantic treats{ann_name!r} as a '

‎tests/test_forward_ref.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
importpytest
99

10-
frompydanticimportBaseModel,PydanticUserError,TypeAdapter,ValidationError
10+
frompydanticimportBaseModel,Field,PydanticUserError,TypeAdapter,ValidationError
1111

1212

1313
deftest_postponed_annotations(create_module):
@@ -1239,11 +1239,11 @@ class SubChild(Child):
12391239
),
12401240
)
12411241
deftest_forward_ref_in_class_parameter()->None:
1242-
"""https://github.com/pydantic/pydantic/issues/11854"""
1242+
"""https://github.com/pydantic/pydantic/issues/11854, https://github.com/pydantic/pydantic/issues/11920"""
12431243
T=TypeVar('T')
12441244

12451245
classModel(BaseModel,Generic[T]):
1246-
f:T
1246+
f:T=Field(json_schema_extra={'extra':'value'})
12471247

12481248
M=Model[list['Undefined']]
12491249

@@ -1253,6 +1253,7 @@ class Model(BaseModel, Generic[T]):
12531253

12541254
assertM.__pydantic_fields_complete__
12551255
assertM.model_fields['f'].annotation==list[int]
1256+
assertM.model_fields['f'].json_schema_extra== {'extra':'value'}
12561257

12571258

12581259
deftest_uses_the_local_namespace_when_generating_schema():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp