Skip to content

Move field_name from runtime to schema generation time #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,7 @@ class GeneralValidatorFunctionSchema(TypedDict):
class FieldValidatorFunctionSchema(TypedDict):
type: Literal['field']
function: FieldValidatorFunction
field_name: str


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

def field_before_validator_function(
function: FieldValidatorFunction,
field_name: str,
schema: CoreSchema,
*,
ref: str | None = None,
Expand All @@ -1710,7 +1712,7 @@ def fn(v: bytes, info: core_schema.FieldValidationInfo) -> str:
return v.decode() + 'world'

func_schema = core_schema.field_before_validator_function(
function=fn, schema=core_schema.str_schema()
function=fn, field_name='a', schema=core_schema.str_schema()
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

Expand All @@ -1720,14 +1722,15 @@ def fn(v: bytes, info: core_schema.FieldValidationInfo) -> str:

Args:
function: The validator function to call
field_name: The name of the field
schema: The schema to validate the output of the validator function
ref: optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
return _dict_not_none(
type='function-before',
function={'type': 'field', 'function': function},
function={'type': 'field', 'function': function, 'field_name': field_name},
schema=schema,
ref=ref,
metadata=metadata,
Expand Down Expand Up @@ -1826,6 +1829,7 @@ def fn(v: str) -> str:

def field_after_validator_function(
function: FieldValidatorFunction,
field_name: str,
schema: CoreSchema,
*,
ref: str | None = None,
Expand All @@ -1845,7 +1849,7 @@ def fn(v: str, info: core_schema.FieldValidationInfo) -> str:
return v + 'world'

func_schema = core_schema.field_after_validator_function(
function=fn, schema=core_schema.str_schema()
function=fn, field_name='a', schema=core_schema.str_schema()
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

Expand All @@ -1855,14 +1859,15 @@ def fn(v: str, info: core_schema.FieldValidationInfo) -> str:

Args:
function: The validator function to call after the schema is validated
field_name: The name of the field
schema: The schema to validate before the validator function
ref: optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
return _dict_not_none(
type='function-after',
function={'type': 'field', 'function': function},
function={'type': 'field', 'function': function, 'field_name': field_name},
schema=schema,
ref=ref,
metadata=metadata,
Expand Down Expand Up @@ -1942,6 +1947,7 @@ class GeneralWrapValidatorFunctionSchema(TypedDict):
class FieldWrapValidatorFunctionSchema(TypedDict):
type: Literal['field']
function: FieldWrapValidatorFunction
field_name: str


WrapValidatorFunction = Union[
Expand Down Expand Up @@ -2053,6 +2059,7 @@ def fn(

def field_wrap_validator_function(
function: FieldWrapValidatorFunction,
field_name: str,
schema: CoreSchema,
*,
ref: str | None = None,
Expand All @@ -2078,7 +2085,7 @@ def fn(
return validator(v) + 'world'

func_schema = core_schema.field_wrap_validator_function(
function=fn, schema=core_schema.str_schema()
function=fn, field_name='a', schema=core_schema.str_schema()
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

Expand All @@ -2088,14 +2095,15 @@ def fn(

Args:
function: The validator function to call
field_name: The name of the field
schema: The schema to validate the output of the validator function
ref: optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
return _dict_not_none(
type='function-wrap',
function={'type': 'field', 'function': function},
function={'type': 'field', 'function': function, 'field_name': field_name},
schema=schema,
ref=ref,
metadata=metadata,
Expand Down Expand Up @@ -2187,6 +2195,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:

def field_plain_validator_function(
function: FieldValidatorFunction,
field_name: str,
*,
ref: str | None = None,
metadata: Any = None,
Expand All @@ -2204,7 +2213,7 @@ def fn(v: Any, info: core_schema.FieldValidationInfo) -> str:
assert info.field_name is not None
return str(v) + 'world'

func_schema = core_schema.field_plain_validator_function(function=fn)
func_schema = core_schema.field_plain_validator_function(function=fn, field_name='a')
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

v = SchemaValidator(schema)
Expand All @@ -2213,13 +2222,14 @@ def fn(v: Any, info: core_schema.FieldValidationInfo) -> str:

Args:
function: The validator function to call
field_name: The name of the field
ref: optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
"""
return _dict_not_none(
type='function-plain',
function={'type': 'field', 'function': function},
function={'type': 'field', 'function': function, 'field_name': field_name},
ref=ref,
metadata=metadata,
serialization=serialization,
Expand Down
5 changes: 0 additions & 5 deletions src/validators/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ impl Validator for DataclassArgsValidator {
($args:ident, $get_method:ident, $get_macro:ident, $slice_macro:ident) => {{
// go through fields getting the value from args or kwargs and validating it
for (index, field) in self.fields.iter().enumerate() {
let extra = Extra {
field_name: Some(&field.name),
..extra
};
let mut pos_value = None;
if let Some(args) = $args.args {
if !field.kw_only {
Expand Down Expand Up @@ -349,7 +345,6 @@ impl Validator for DataclassArgsValidator {
}
}
let next_extra = Extra {
field_name: Some(field_name),
data: Some(data_dict),
..*extra
};
Expand Down
Loading