Skip to content

Commit 9bac3bd

Browse files
committed
Deprecate field_name argument on core_schema.with_info_{before,after,plain,wrap}_validator_function
1 parent 73c2362 commit 9bac3bd

File tree

7 files changed

+65
-89
lines changed

7 files changed

+65
-89
lines changed

python/pydantic_core/core_schema.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,7 @@ class NoInfoValidatorFunctionSchema(TypedDict):
19541954
class WithInfoValidatorFunctionSchema(TypedDict, total=False):
19551955
type: Required[Literal['with-info']]
19561956
function: Required[WithInfoValidatorFunction]
1957-
field_name: str
1957+
field_name: str # deprecated
19581958

19591959

19601960
ValidationFunction = Union[NoInfoValidatorFunctionSchema, WithInfoValidatorFunctionSchema]
@@ -2042,7 +2042,7 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
20422042
return v.decode() + 'world'
20432043
20442044
func_schema = core_schema.with_info_before_validator_function(
2045-
function=fn, schema=core_schema.str_schema(), field_name='a'
2045+
function=fn, schema=core_schema.str_schema()
20462046
)
20472047
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
20482048
@@ -2052,13 +2052,19 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
20522052
20532053
Args:
20542054
function: The validator function to call
2055-
field_name: The name of the field
2055+
field_name: The name of the field this validator is applied to, if any (deprecated)
20562056
schema: The schema to validate the output of the validator function
20572057
ref: optional unique identifier of the schema, used to reference the schema in other places
20582058
json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type
20592059
metadata: Any other information you want to include with the schema, not used by pydantic-core
20602060
serialization: Custom serialization schema
20612061
"""
2062+
if field_name is not None:
2063+
warnings.warn(
2064+
'The `field_name` argument on `with_info_before_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.',
2065+
DeprecationWarning,
2066+
)
2067+
20622068
return _dict_not_none(
20632069
type='function-before',
20642070
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
@@ -2140,7 +2146,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
21402146
return v + 'world'
21412147
21422148
func_schema = core_schema.with_info_after_validator_function(
2143-
function=fn, schema=core_schema.str_schema(), field_name='a'
2149+
function=fn, schema=core_schema.str_schema()
21442150
)
21452151
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
21462152
@@ -2151,11 +2157,17 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
21512157
Args:
21522158
function: The validator function to call after the schema is validated
21532159
schema: The schema to validate before the validator function
2154-
field_name: The name of the field this validators is applied to, if any
2160+
field_name: The name of the field this validator is applied to, if any (deprecated)
21552161
ref: optional unique identifier of the schema, used to reference the schema in other places
21562162
metadata: Any other information you want to include with the schema, not used by pydantic-core
21572163
serialization: Custom serialization schema
21582164
"""
2165+
if field_name is not None:
2166+
warnings.warn(
2167+
'The `field_name` argument on `with_info_after_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.',
2168+
DeprecationWarning,
2169+
)
2170+
21592171
return _dict_not_none(
21602172
type='function-after',
21612173
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
@@ -2287,12 +2299,18 @@ def fn(
22872299
Args:
22882300
function: The validator function to call
22892301
schema: The schema to validate the output of the validator function
2290-
field_name: The name of the field this validators is applied to, if any
2302+
field_name: The name of the field this validator is applied to, if any (deprecated)
22912303
json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type
22922304
ref: optional unique identifier of the schema, used to reference the schema in other places
22932305
metadata: Any other information you want to include with the schema, not used by pydantic-core
22942306
serialization: Custom serialization schema
22952307
"""
2308+
if field_name is not None:
2309+
warnings.warn(
2310+
'The `field_name` argument on `with_info_wrap_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.',
2311+
DeprecationWarning,
2312+
)
2313+
22962314
return _dict_not_none(
22972315
type='function-wrap',
22982316
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
@@ -2379,12 +2397,18 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
23792397
23802398
Args:
23812399
function: The validator function to call
2382-
field_name: The name of the field this validators is applied to, if any
2400+
field_name: The name of the field this validator is applied to, if any (deprecated)
23832401
ref: optional unique identifier of the schema, used to reference the schema in other places
23842402
json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type
23852403
metadata: Any other information you want to include with the schema, not used by pydantic-core
23862404
serialization: Custom serialization schema
23872405
"""
2406+
if field_name is not None:
2407+
warnings.warn(
2408+
'The `field_name` argument on `with_info_plain_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.',
2409+
DeprecationWarning,
2410+
)
2411+
23882412
return _dict_not_none(
23892413
type='function-plain',
23902414
function=_dict_not_none(type='with-info', function=function, field_name=field_name),

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ def f(v: int, info: core_schema.ValidationInfo) -> int:
13481348
limit = pydantic_core._pydantic_core._recursion_limit - 3
13491349

13501350
for _ in range(limit):
1351-
schema = core_schema.with_info_after_validator_function(f, schema, field_name='x')
1351+
schema = core_schema.with_info_after_validator_function(f, schema)
13521352

13531353
schema = core_schema.typed_dict_schema({'x': core_schema.typed_dict_field(schema)})
13541354

tests/validators/test_dataclasses.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,7 @@ def validate_b(cls, v: str, info: core_schema.ValidationInfo) -> str:
506506
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
507507
core_schema.dataclass_field(
508508
name='b',
509-
schema=core_schema.with_info_after_validator_function(
510-
Foo.validate_b, core_schema.str_schema(), field_name='b'
511-
),
509+
schema=core_schema.with_info_after_validator_function(Foo.validate_b, core_schema.str_schema()),
512510
),
513511
],
514512
),
@@ -540,7 +538,7 @@ def validate_b(cls, v: bytes, info: core_schema.ValidationInfo) -> str:
540538
[
541539
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
542540
core_schema.dataclass_field(
543-
name='b', schema=core_schema.with_info_plain_validator_function(Foo.validate_b, field_name='b')
541+
name='b', schema=core_schema.with_info_plain_validator_function(Foo.validate_b)
544542
),
545543
],
546544
),
@@ -573,9 +571,7 @@ def validate_b(cls, v: bytes, info: core_schema.ValidationInfo) -> bytes:
573571
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
574572
core_schema.dataclass_field(
575573
name='b',
576-
schema=core_schema.with_info_before_validator_function(
577-
Foo.validate_b, core_schema.str_schema(), field_name='b'
578-
),
574+
schema=core_schema.with_info_before_validator_function(Foo.validate_b, core_schema.str_schema()),
579575
),
580576
],
581577
),
@@ -612,9 +608,7 @@ def validate_b(
612608
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
613609
core_schema.dataclass_field(
614610
name='b',
615-
schema=core_schema.with_info_wrap_validator_function(
616-
Foo.validate_b, core_schema.str_schema(), field_name='b'
617-
),
611+
schema=core_schema.with_info_wrap_validator_function(Foo.validate_b, core_schema.str_schema()),
618612
),
619613
],
620614
),
@@ -649,9 +643,7 @@ def validate_b(
649643
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
650644
core_schema.dataclass_field(
651645
name='b',
652-
schema=core_schema.with_info_wrap_validator_function(
653-
Foo.validate_b, core_schema.str_schema(), field_name='b'
654-
),
646+
schema=core_schema.with_info_wrap_validator_function(Foo.validate_b, core_schema.str_schema()),
655647
),
656648
],
657649
),
@@ -878,9 +870,7 @@ def func(x, info):
878870
core_schema.dataclass_field('field_a', core_schema.str_schema()),
879871
core_schema.dataclass_field(
880872
'field_b',
881-
core_schema.with_info_after_validator_function(
882-
func, core_schema.int_schema(), field_name='field_b'
883-
),
873+
core_schema.with_info_after_validator_function(func, core_schema.int_schema()),
884874
),
885875
core_schema.dataclass_field('field_c', core_schema.int_schema()),
886876
],
@@ -1295,9 +1285,7 @@ def validate_b(cls, v: bytes, info: core_schema.ValidationInfo) -> bytes:
12951285
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
12961286
core_schema.dataclass_field(
12971287
name='b',
1298-
schema=core_schema.with_info_before_validator_function(
1299-
Foo.validate_b, core_schema.str_schema(), field_name='b'
1300-
),
1288+
schema=core_schema.with_info_before_validator_function(Foo.validate_b, core_schema.str_schema()),
13011289
),
13021290
],
13031291
),
@@ -1332,9 +1320,7 @@ def validate_b(cls, v: str, info: core_schema.ValidationInfo) -> str:
13321320
core_schema.dataclass_field(name='a', schema=core_schema.int_schema()),
13331321
core_schema.dataclass_field(
13341322
name='b',
1335-
schema=core_schema.with_info_after_validator_function(
1336-
Foo.validate_b, core_schema.str_schema(), field_name='b'
1337-
),
1323+
schema=core_schema.with_info_after_validator_function(Foo.validate_b, core_schema.str_schema()),
13381324
),
13391325
],
13401326
),
@@ -1550,15 +1536,9 @@ def _wrap_validator(cls, v, validator, info):
15501536

15511537
field_schema = core_schema.int_schema()
15521538
if validator == 'field':
1553-
field_schema = core_schema.with_info_before_validator_function(
1554-
Dataclass._validator, field_schema, field_name='a'
1555-
)
1556-
field_schema = core_schema.with_info_wrap_validator_function(
1557-
Dataclass._wrap_validator, field_schema, field_name='a'
1558-
)
1559-
field_schema = core_schema.with_info_after_validator_function(
1560-
Dataclass._validator, field_schema, field_name='a'
1561-
)
1539+
field_schema = core_schema.with_info_before_validator_function(Dataclass._validator, field_schema)
1540+
field_schema = core_schema.with_info_wrap_validator_function(Dataclass._wrap_validator, field_schema)
1541+
field_schema = core_schema.with_info_after_validator_function(Dataclass._validator, field_schema)
15621542

15631543
dataclass_schema = core_schema.dataclass_schema(
15641544
Dataclass,

tests/validators/test_function.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
605605
core_schema.model_fields_schema(
606606
{
607607
'x': core_schema.model_field(
608-
core_schema.with_info_before_validator_function(f, core_schema.str_schema(), field_name='x')
608+
core_schema.with_info_before_validator_function(f, core_schema.str_schema())
609609
)
610610
}
611611
),
@@ -631,7 +631,7 @@ def f(input_value: str, info: core_schema.ValidationInfo) -> Any:
631631
core_schema.model_fields_schema(
632632
{
633633
'x': core_schema.model_field(
634-
core_schema.with_info_after_validator_function(f, core_schema.str_schema(), field_name='x')
634+
core_schema.with_info_after_validator_function(f, core_schema.str_schema())
635635
)
636636
}
637637
),
@@ -655,7 +655,7 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
655655
core_schema.model_schema(
656656
Model,
657657
core_schema.model_fields_schema(
658-
{'x': core_schema.model_field(core_schema.with_info_plain_validator_function(f, field_name='x'))}
658+
{'x': core_schema.model_field(core_schema.with_info_plain_validator_function(f))}
659659
),
660660
)
661661
)
@@ -674,7 +674,10 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
674674
# When a type alias with a validator function is used on multiple fields,
675675
# its core schema is only generated once (with the first field_name) and reused.
676676
# See https://github.com/pydantic/pydantic/issues/11737
677-
validator = core_schema.with_info_plain_validator_function(f, field_name='x')
677+
with pytest.warns(
678+
DeprecationWarning, match='`field_name` argument on `with_info_plain_validator_function` is deprecated'
679+
):
680+
validator = core_schema.with_info_plain_validator_function(f, field_name='x')
678681

679682
v = SchemaValidator(
680683
core_schema.model_schema(
@@ -709,7 +712,7 @@ def f(input_value: Any, val: core_schema.ValidatorFunctionWrapHandler, info: cor
709712
core_schema.model_fields_schema(
710713
{
711714
'x': core_schema.model_field(
712-
core_schema.with_info_wrap_validator_function(f, core_schema.str_schema(), field_name='x')
715+
core_schema.with_info_wrap_validator_function(f, core_schema.str_schema())
713716
)
714717
}
715718
),
@@ -833,7 +836,7 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
833836
'a': core_schema.typed_dict_field(core_schema.int_schema()),
834837
'b': core_schema.typed_dict_field(core_schema.int_schema()),
835838
'c': core_schema.typed_dict_field(
836-
core_schema.with_info_after_validator_function(f, core_schema.str_schema(), field_name='c')
839+
core_schema.with_info_after_validator_function(f, core_schema.str_schema())
837840
),
838841
}
839842
)
@@ -858,7 +861,10 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
858861
# When a type alias with a validator function is used on multiple fields,
859862
# its core schema is only generated once (with the first field_name) and reused.
860863
# See https://github.com/pydantic/pydantic/issues/11737
861-
validator = core_schema.with_info_plain_validator_function(f, field_name='x')
864+
with pytest.warns(
865+
DeprecationWarning, match='`field_name` argument on `with_info_plain_validator_function` is deprecated'
866+
):
867+
validator = core_schema.with_info_plain_validator_function(f, field_name='x')
862868

863869
v = SchemaValidator(
864870
core_schema.typed_dict_schema(
@@ -886,7 +892,10 @@ def f(input_value: Any, info: core_schema.ValidationInfo) -> Any:
886892
# When a type alias with a validator function is used on multiple fields,
887893
# its core schema is only generated once (with the first field_name) and reused.
888894
# See https://github.com/pydantic/pydantic/issues/11737
889-
validator = core_schema.with_info_plain_validator_function(f, field_name='x')
895+
with pytest.warns(
896+
DeprecationWarning, match='`field_name` argument on `with_info_plain_validator_function` is deprecated'
897+
):
898+
validator = core_schema.with_info_plain_validator_function(f, field_name='x')
890899

891900
v = SchemaValidator(
892901
core_schema.dataclass_schema(
@@ -1002,35 +1011,6 @@ def f_w(v: Any, handler: core_schema.ValidatorFunctionWrapHandler, info: core_sc
10021011
calls.clear()
10031012

10041013

1005-
def test_reprs() -> None:
1006-
reprs: list[str] = []
1007-
1008-
def sample_repr(v: Any, info: core_schema.ValidationInfo) -> Any:
1009-
reprs.append(repr(info))
1010-
return v
1011-
1012-
v = SchemaValidator(
1013-
core_schema.chain_schema(
1014-
[
1015-
core_schema.with_info_plain_validator_function(sample_repr),
1016-
core_schema.with_info_plain_validator_function(sample_repr, field_name='x'),
1017-
]
1018-
)
1019-
)
1020-
1021-
class Foo:
1022-
def __repr__(self) -> str:
1023-
return 'This is Foo!'
1024-
1025-
v.validate_python(Foo())
1026-
1027-
# insert_assert(reprs)
1028-
assert reprs == [
1029-
'ValidationInfo(config=None, context=None, data=None, field_name=None)',
1030-
"ValidationInfo(config=None, context=None, data=None, field_name='x')",
1031-
]
1032-
1033-
10341014
def test_function_after_doesnt_change_mode() -> None:
10351015
# https://github.com/pydantic/pydantic/issues/7468 - function-after was
10361016
# incorrectly forcing Python validation mode

tests/validators/test_model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,7 @@ def func(x, info):
10541054
{
10551055
'field_a': core_schema.model_field(core_schema.str_schema()),
10561056
'field_b': core_schema.model_field(
1057-
core_schema.with_info_after_validator_function(
1058-
func, core_schema.int_schema(), field_name='field_b'
1059-
)
1057+
core_schema.with_info_after_validator_function(func, core_schema.int_schema())
10601058
),
10611059
'field_c': core_schema.model_field(core_schema.int_schema()),
10621060
}

tests/validators/test_model_init.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,9 @@ def _wrap_validator(cls, v, validator, info):
430430

431431
field_schema = core_schema.int_schema()
432432
if validator == 'field':
433-
field_schema = core_schema.with_info_before_validator_function(
434-
Model._validator, field_schema, field_name='a'
435-
)
436-
field_schema = core_schema.with_info_wrap_validator_function(
437-
Model._wrap_validator, field_schema, field_name='a'
438-
)
439-
field_schema = core_schema.with_info_after_validator_function(
440-
Model._validator, field_schema, field_name='a'
441-
)
433+
field_schema = core_schema.with_info_before_validator_function(Model._validator, field_schema)
434+
field_schema = core_schema.with_info_wrap_validator_function(Model._wrap_validator, field_schema)
435+
field_schema = core_schema.with_info_after_validator_function(Model._validator, field_schema)
442436

443437
model_schema = core_schema.model_schema(
444438
Model, core_schema.model_fields_schema({'a': core_schema.model_field(field_schema)})

tests/validators/test_model_root.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def f(input_value: str, info):
138138
v = SchemaValidator(
139139
core_schema.model_schema(
140140
RootModel,
141-
core_schema.with_info_after_validator_function(f, core_schema.str_schema(), field_name='root'),
141+
core_schema.with_info_after_validator_function(f, core_schema.str_schema()),
142142
root_model=True,
143143
)
144144
)

0 commit comments

Comments
 (0)