Skip to content

Commit 024efa2

Browse files
authored
Move field_name from runtime to schema generation time (#715)
1 parent d76812a commit 024efa2

File tree

13 files changed

+113
-118
lines changed

13 files changed

+113
-118
lines changed

python/pydantic_core/core_schema.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,7 @@ class GeneralValidatorFunctionSchema(TypedDict):
16291629
class FieldValidatorFunctionSchema(TypedDict):
16301630
type: Literal['field']
16311631
function: FieldValidatorFunction
1632+
field_name: str
16321633

16331634

16341635
ValidationFunction = Union[NoInfoValidatorFunctionSchema, FieldValidatorFunctionSchema, GeneralValidatorFunctionSchema]
@@ -1691,6 +1692,7 @@ def fn(v: bytes) -> str:
16911692

16921693
def field_before_validator_function(
16931694
function: FieldValidatorFunction,
1695+
field_name: str,
16941696
schema: CoreSchema,
16951697
*,
16961698
ref: str | None = None,
@@ -1710,7 +1712,7 @@ def fn(v: bytes, info: core_schema.FieldValidationInfo) -> str:
17101712
return v.decode() + 'world'
17111713
17121714
func_schema = core_schema.field_before_validator_function(
1713-
function=fn, schema=core_schema.str_schema()
1715+
function=fn, field_name='a', schema=core_schema.str_schema()
17141716
)
17151717
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
17161718
@@ -1720,14 +1722,15 @@ def fn(v: bytes, info: core_schema.FieldValidationInfo) -> str:
17201722
17211723
Args:
17221724
function: The validator function to call
1725+
field_name: The name of the field
17231726
schema: The schema to validate the output of the validator function
17241727
ref: optional unique identifier of the schema, used to reference the schema in other places
17251728
metadata: Any other information you want to include with the schema, not used by pydantic-core
17261729
serialization: Custom serialization schema
17271730
"""
17281731
return _dict_not_none(
17291732
type='function-before',
1730-
function={'type': 'field', 'function': function},
1733+
function={'type': 'field', 'function': function, 'field_name': field_name},
17311734
schema=schema,
17321735
ref=ref,
17331736
metadata=metadata,
@@ -1826,6 +1829,7 @@ def fn(v: str) -> str:
18261829

18271830
def field_after_validator_function(
18281831
function: FieldValidatorFunction,
1832+
field_name: str,
18291833
schema: CoreSchema,
18301834
*,
18311835
ref: str | None = None,
@@ -1845,7 +1849,7 @@ def fn(v: str, info: core_schema.FieldValidationInfo) -> str:
18451849
return v + 'world'
18461850
18471851
func_schema = core_schema.field_after_validator_function(
1848-
function=fn, schema=core_schema.str_schema()
1852+
function=fn, field_name='a', schema=core_schema.str_schema()
18491853
)
18501854
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
18511855
@@ -1855,14 +1859,15 @@ def fn(v: str, info: core_schema.FieldValidationInfo) -> str:
18551859
18561860
Args:
18571861
function: The validator function to call after the schema is validated
1862+
field_name: The name of the field
18581863
schema: The schema to validate before the validator function
18591864
ref: optional unique identifier of the schema, used to reference the schema in other places
18601865
metadata: Any other information you want to include with the schema, not used by pydantic-core
18611866
serialization: Custom serialization schema
18621867
"""
18631868
return _dict_not_none(
18641869
type='function-after',
1865-
function={'type': 'field', 'function': function},
1870+
function={'type': 'field', 'function': function, 'field_name': field_name},
18661871
schema=schema,
18671872
ref=ref,
18681873
metadata=metadata,
@@ -1942,6 +1947,7 @@ class GeneralWrapValidatorFunctionSchema(TypedDict):
19421947
class FieldWrapValidatorFunctionSchema(TypedDict):
19431948
type: Literal['field']
19441949
function: FieldWrapValidatorFunction
1950+
field_name: str
19451951

19461952

19471953
WrapValidatorFunction = Union[
@@ -2053,6 +2059,7 @@ def fn(
20532059

20542060
def field_wrap_validator_function(
20552061
function: FieldWrapValidatorFunction,
2062+
field_name: str,
20562063
schema: CoreSchema,
20572064
*,
20582065
ref: str | None = None,
@@ -2078,7 +2085,7 @@ def fn(
20782085
return validator(v) + 'world'
20792086
20802087
func_schema = core_schema.field_wrap_validator_function(
2081-
function=fn, schema=core_schema.str_schema()
2088+
function=fn, field_name='a', schema=core_schema.str_schema()
20822089
)
20832090
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
20842091
@@ -2088,14 +2095,15 @@ def fn(
20882095
20892096
Args:
20902097
function: The validator function to call
2098+
field_name: The name of the field
20912099
schema: The schema to validate the output of the validator function
20922100
ref: optional unique identifier of the schema, used to reference the schema in other places
20932101
metadata: Any other information you want to include with the schema, not used by pydantic-core
20942102
serialization: Custom serialization schema
20952103
"""
20962104
return _dict_not_none(
20972105
type='function-wrap',
2098-
function={'type': 'field', 'function': function},
2106+
function={'type': 'field', 'function': function, 'field_name': field_name},
20992107
schema=schema,
21002108
ref=ref,
21012109
metadata=metadata,
@@ -2187,6 +2195,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
21872195

21882196
def field_plain_validator_function(
21892197
function: FieldValidatorFunction,
2198+
field_name: str,
21902199
*,
21912200
ref: str | None = None,
21922201
metadata: Any = None,
@@ -2204,7 +2213,7 @@ def fn(v: Any, info: core_schema.FieldValidationInfo) -> str:
22042213
assert info.field_name is not None
22052214
return str(v) + 'world'
22062215
2207-
func_schema = core_schema.field_plain_validator_function(function=fn)
2216+
func_schema = core_schema.field_plain_validator_function(function=fn, field_name='a')
22082217
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
22092218
22102219
v = SchemaValidator(schema)
@@ -2213,13 +2222,14 @@ def fn(v: Any, info: core_schema.FieldValidationInfo) -> str:
22132222
22142223
Args:
22152224
function: The validator function to call
2225+
field_name: The name of the field
22162226
ref: optional unique identifier of the schema, used to reference the schema in other places
22172227
metadata: Any other information you want to include with the schema, not used by pydantic-core
22182228
serialization: Custom serialization schema
22192229
"""
22202230
return _dict_not_none(
22212231
type='function-plain',
2222-
function={'type': 'field', 'function': function},
2232+
function={'type': 'field', 'function': function, 'field_name': field_name},
22232233
ref=ref,
22242234
metadata=metadata,
22252235
serialization=serialization,

src/validators/dataclass.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ impl Validator for DataclassArgsValidator {
162162
($args:ident, $get_method:ident, $get_macro:ident, $slice_macro:ident) => {{
163163
// go through fields getting the value from args or kwargs and validating it
164164
for (index, field) in self.fields.iter().enumerate() {
165-
let extra = Extra {
166-
field_name: Some(&field.name),
167-
..extra
168-
};
169165
let mut pos_value = None;
170166
if let Some(args) = $args.args {
171167
if !field.kw_only {
@@ -349,7 +345,6 @@ impl Validator for DataclassArgsValidator {
349345
}
350346
}
351347
let next_extra = Extra {
352-
field_name: Some(field_name),
353348
data: Some(data_dict),
354349
..*extra
355350
};

0 commit comments

Comments
 (0)