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

Commite92ad79

Browse files
authored
Check ifFieldInfo is complete after applying type variable map (#11855)
1 parent944674a commite92ad79

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

‎pydantic/_internal/_fields.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ def rebuild_model_fields(
411411
412412
This function should be called whenever a model with incomplete fields is encountered.
413413
414+
Raises:
415+
NameError: If one of the annotations failed to evaluate.
416+
414417
Note:
415418
This function *doesn't* mutate the model fields in place, as it can be called during
416419
schema generation, where you don't want to mutate other model's fields.

‎pydantic/fields.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,12 @@ def apply_typevars_map(
693693
pydantic._internal._generics.replace_types is used for replacing the typevars with
694694
their concrete types.
695695
"""
696-
annotation,_=_typing_extra.try_eval_type(self.annotation,globalns,localns)
697-
self.annotation=_generics.replace_types(annotation,typevars_map)
696+
annotation=_generics.replace_types(self.annotation,typevars_map)
697+
annotation,evaluated=_typing_extra.try_eval_type(annotation,globalns,localns)
698+
self.annotation=annotation
699+
ifnotevaluated:
700+
self._complete=False
701+
self._original_annotation=self.annotation
698702

699703
def__repr_args__(self)->ReprArgs:
700704
yield'annotation',_repr.PlainRepr(_repr.display_as_type(self.annotation))

‎tests/test_forward_ref.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
importre
44
importsys
55
importtyping
6-
fromtypingimportAny,Optional
6+
fromtypingimportAny,Generic,Optional,TypeVar
77

88
importpytest
99

@@ -1232,6 +1232,29 @@ class SubChild(Child):
12321232
)
12331233

12341234

1235+
@pytest.mark.skipif(
1236+
sys.version_info< (3,11),
1237+
reason=(
1238+
'Forward refs inside PEP 585 generics are not evaluated (see https://github.com/python/cpython/pull/30900).'
1239+
),
1240+
)
1241+
deftest_forward_ref_in_class_parameter()->None:
1242+
"""https://github.com/pydantic/pydantic/issues/11854"""
1243+
T=TypeVar('T')
1244+
1245+
classModel(BaseModel,Generic[T]):
1246+
f:T
1247+
1248+
M=Model[list['Undefined']]
1249+
1250+
assertnotM.__pydantic_fields_complete__
1251+
1252+
M.model_rebuild(_types_namespace={'Undefined':int})
1253+
1254+
assertM.__pydantic_fields_complete__
1255+
assertM.model_fields['f'].annotation==list[int]
1256+
1257+
12351258
deftest_uses_the_local_namespace_when_generating_schema():
12361259
deffunc():
12371260
A=int

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp