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

Commit638d047

Browse files
authored
Do not use deprecated methods as default field values (#11914)
1 parent9d6b2bf commit638d047

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

‎pydantic/_internal/_fields.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ def update_field_from_config(config_wrapper: ConfigWrapper, field_name: str, fie
199199
_apply_alias_generator_to_field_info(config_wrapper.alias_generator,field_name,field_info)
200200

201201

202+
_deprecated_method_names= {'dict','json','copy','_iter','_copy_and_set_values','_calculate_keys'}
203+
204+
_deprecated_classmethod_names= {
205+
'parse_obj',
206+
'parse_raw',
207+
'parse_file',
208+
'from_orm',
209+
'construct',
210+
'schema',
211+
'schema_json',
212+
'validate',
213+
'update_forward_refs',
214+
'_get_value',
215+
}
216+
217+
202218
defcollect_model_fields(# noqa: C901
203219
cls:type[BaseModel],
204220
config_wrapper:ConfigWrapper,
@@ -231,6 +247,7 @@ def collect_model_fields( # noqa: C901
231247
- If a field shadows an attribute in the parent model.
232248
"""
233249
FieldInfo_=import_cached_field_info()
250+
BaseModel_=import_cached_base_model()
234251

235252
bases=cls.__bases__
236253
parent_fields_lookup:dict[str,FieldInfo]= {}
@@ -265,6 +282,20 @@ def collect_model_fields( # noqa: C901
265282
continue
266283

267284
assigned_value=getattr(cls,ann_name,PydanticUndefined)
285+
ifassigned_valueisnotPydanticUndefinedand (
286+
# One of the deprecated instance methods was used as a field name (e.g. `dict()`):
287+
any(getattr(BaseModel_,depr_name,None)isassigned_valuefordepr_namein_deprecated_method_names)
288+
# One of the deprecated class methods was used as a field name (e.g. `schema()`):
289+
or (
290+
hasattr(assigned_value,'__func__')
291+
andany(
292+
getattr(getattr(BaseModel_,depr_name,None),'__func__',None)isassigned_value.__func__# pyright: ignore[reportAttributeAccessIssue]
293+
fordepr_namein_deprecated_classmethod_names
294+
)
295+
)
296+
):
297+
# Then `assigned_value` would be the method, even though no default was specified:
298+
assigned_value=PydanticUndefined
268299

269300
ifnotis_valid_field_name(ann_name):
270301
continue

‎tests/test_main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,21 @@ class ChildWithRedefinedField(BaseModel, Parent):
34983498
foo:bool=True
34993499

35003500

3501+
deftest_field_name_deprecated_method_name()->None:
3502+
"""https://github.com/pydantic/pydantic/issues/11912"""
3503+
3504+
withpytest.warns(UserWarning):
3505+
3506+
classModel(BaseModel):
3507+
# `collect_model_fields()` will special case these to not use
3508+
# the deprecated methods as default values:
3509+
dict:int
3510+
schema:str
3511+
3512+
assertModel.model_fields['dict'].is_required()
3513+
assertModel.model_fields['schema'].is_required()
3514+
3515+
35013516
deftest_eval_type_backport():
35023517
classModel(BaseModel):
35033518
foo:'list[int | str]'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp