8000 Always store the original field assignment on `FieldInfo` · pydantic/pydantic@dc7a9d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc7a9d2

Browse files
committed
Always store the original field assignment on FieldInfo
Backport of: #11946
1 parent c284c27 commit dc7a9d2

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
@@ -235,12 +235,13 @@ def collect_model_fields( # noqa: C901
235235
)
236236

237237
field_info = FieldInfo_.from_annotated_attribute(ann_type, assigned_value, _source=AnnotationSource.CLASS)
238+
# Store the original annotation and assignment value that should be used to rebuild the field info later.
239+
# Note that the assignment is always stored as the annotation might contain a type var that is later
240+
# parameterized with an unknown forward reference (and we'll need it to rebuild the field info):
241+
field_info._original_assignment = original_assignment
238242
if not evaluated:
239243
field_info._complete = False
240-
# Store the original annotation and assignment value that should be used to rebuild
241-
# the field info later:
242244
field_info._original_annotation = ann_type
243-
field_info._original_assignment = original_assignment
244245
elif 'final' in field_info._qualifiers and not field_info.is_required():
245246
warnings.warn(
246247
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
import pytest
99

10-
from pydantic import BaseModel, PydanticUserError, TypeAdapter, ValidationError
10+
from pydantic import BaseModel, Field, PydanticUserError, TypeAdapter, ValidationError
1111

1212

1313
def test_postponed_annotations(create_module):
@@ -1239,11 +1239,11 @@ class SubChild(Child):
12391239
),
12401240
)
12411241
def test_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
class Model(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
assert M.__pydantic_fields_complete__
12551255
assert M.model_fields['f'].annotation == list[int]
1256+
assert M.model_fields['f'].json_schema_extra == {'extra': 'value'}
12561257

12571258

12581259
def test_uses_the_local_namespace_when_generating_schema():

0 commit comments

Comments
 (0)
0