Skip to content

Add field_name to ValidatorInfo #439

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 26 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
359 changes: 316 additions & 43 deletions pydantic_core/core_schema.py

Large diffs are not rendered by default.

82 changes: 67 additions & 15 deletions src/validators/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pyo3::exceptions::{PyAssertionError, PyTypeError, PyValueError};
use pyo3::exceptions::{PyAssertionError, PyAttributeError, PyRuntimeError, PyTypeError, PyValueError};
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyDict};
use pyo3::types::{PyAny, PyDict, PyString};

use crate::build_tools::{function_name, py_err, SchemaDict};
use crate::errors::{
Expand All @@ -16,6 +16,18 @@ use super::{build_validator, BuildContext, BuildValidator, CombinedValidator, Ex

pub struct FunctionBuilder;

fn destructure_function_schema(schema: &PyDict) -> PyResult<(bool, &PyAny)> {
let func_dict: &PyDict = schema.get_as_req(intern!(schema.py(), "function"))?;
let function: &PyAny = func_dict.get_as_req(intern!(schema.py(), "function"))?;
let func_type: &str = func_dict.get_as_req(intern!(schema.py(), "type"))?;
let is_field_validator = match func_type {
"field" => true,
"general" => false,
_ => unreachable!(),
};
Ok((is_field_validator, function))
}

impl BuildValidator for FunctionBuilder {
const EXPECTED_TYPE: &'static str = "function";

Expand Down Expand Up @@ -45,7 +57,7 @@ macro_rules! impl_build {
) -> PyResult<CombinedValidator> {
let py = schema.py();
let validator = build_validator(schema.get_as_req(intern!(py, "schema"))?, config, build_context)?;
let function = schema.get_as_req::<&PyAny>(intern!(py, "function"))?;
let (is_field_validator, function) = destructure_function_schema(schema)?;
let name = format!(
"{}[{}(), {}]",
$name,
Expand All @@ -60,6 +72,7 @@ macro_rules! impl_build {
None => py.None(),
},
name,
is_field_validator,
}
.into())
}
Expand All @@ -73,6 +86,7 @@ pub struct FunctionBeforeValidator {
func: PyObject,
config: PyObject,
name: String,
is_field_validator: bool,
}

impl_build!(FunctionBeforeValidator, "function-before");
Expand All @@ -86,7 +100,7 @@ impl Validator for FunctionBeforeValidator {
slots: &'data [CombinedValidator],
recursion_guard: &'s mut RecursionGuard,
) -> ValResult<'data, PyObject> {
let info = ValidationInfo::new(py, extra, &self.config);
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
let value = self
.func
.call1(py, (input.to_object(py), info))
Expand Down Expand Up @@ -115,6 +129,7 @@ pub struct FunctionAfterValidator {
func: PyObject,
config: PyObject,
name: String,
is_field_validator: bool,
}

impl_build!(FunctionAfterValidator, "function-after");
Expand All @@ -129,7 +144,7 @@ impl Validator for FunctionAfterValidator {
recursion_guard: &'s mut RecursionGuard,
) -> ValResult<'data, PyObject> {
let v = self.validator.validate(py, input, extra, slots, recursion_guard)?;
let info = ValidationInfo::new(py, extra, &self.config);
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
self.func.call1(py, (v, info)).map_err(|e| convert_err(py, e, input))
}

Expand All @@ -151,19 +166,21 @@ pub struct FunctionPlainValidator {
func: PyObject,
config: PyObject,
name: String,
is_field_validator: bool,
}

impl FunctionPlainValidator {
pub fn build(schema: &PyDict, config: Option<&PyDict>) -> PyResult<CombinedValidator> {
let py = schema.py();
let function = schema.get_as_req::<&PyAny>(intern!(py, "function"))?;
let (is_field_validator, function) = destructure_function_schema(schema)?;
Ok(Self {
func: function.into_py(py),
config: match config {
Some(c) => c.into(),
None => py.None(),
},
name: format!("function-plain[{}()]", function_name(function)?),
is_field_validator,
}
.into())
}
Expand All @@ -178,7 +195,7 @@ impl Validator for FunctionPlainValidator {
_slots: &'data [CombinedValidator],
_recursion_guard: &'s mut RecursionGuard,
) -> ValResult<'data, PyObject> {
let info = ValidationInfo::new(py, extra, &self.config);
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
self.func
.call1(py, (input.to_object(py), info))
.map_err(|e| convert_err(py, e, input))
Expand All @@ -195,6 +212,7 @@ pub struct FunctionWrapValidator {
func: PyObject,
config: PyObject,
name: String,
is_field_validator: bool,
}

impl_build!(FunctionWrapValidator, "function-wrap");
Expand All @@ -211,7 +229,7 @@ impl Validator for FunctionWrapValidator {
let call_next_validator = ValidatorCallable {
validator: InternalValidator::new(py, "ValidatorCallable", &self.validator, slots, extra, recursion_guard),
};
let info = ValidationInfo::new(py, extra, &self.config);
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
self.func
.call1(py, (input.to_object(py), call_next_validator, info))
.map_err(|e| convert_err(py, e, input))
Expand Down Expand Up @@ -300,20 +318,54 @@ pub fn convert_err<'a>(py: Python<'a>, err: PyErr, input: &'a impl Input<'a>) ->

#[pyclass(module = "pydantic_core._pydantic_core")]
pub struct ValidationInfo {
#[pyo3(get)]
data: Option<Py<PyDict>>,
#[pyo3(get)]
config: PyObject,
#[pyo3(get)]
context: Option<PyObject>,
data: Option<Py<PyDict>>,
field_name: Option<String>,
}

impl ValidationInfo {
fn new(py: Python, extra: &Extra, config: &PyObject) -> Self {
Self {
data: extra.data.map(|v| v.into()),
config: config.clone_ref(py),
context: extra.context.map(|v| v.into()),
fn new(py: Python, extra: &Extra, config: &PyObject, is_field_validator: bool) -> PyResult<Self> {
if is_field_validator {
match extra.field_name {
Some(field_name) => Ok(
Self {
config: config.clone_ref(py),
context: extra.context.map(|v| v.into()),
field_name: Some(field_name.to_string()),
data: extra.data.map(|v| v.into()),
}
),
_ => Err(PyRuntimeError::new_err("This validator expected to be run inside the context of a model field but no model field was found")),
}
} else {
Ok(Self {
config: config.clone_ref(py),
context: extra.context.map(|v| v.into()),
field_name: None,
data: None,
})
}
}
}

#[pymethods]
impl ValidationInfo {
#[getter]
fn get_data(&self, py: Python) -> PyResult<Py<PyDict>> {
match self.data {
Some(ref data) => Ok(data.clone_ref(py)),
None => Err(PyAttributeError::new_err("No attribute named 'data'")),
}
}

#[getter]
fn get_field_name<'py>(&self, py: Python<'py>) -> PyResult<&'py PyString> {
match self.field_name {
Some(ref field_name) => Ok(PyString::new(py, field_name)),
None => Err(PyAttributeError::new_err("No attribute named 'field_name'")),
}
}
}
5 changes: 3 additions & 2 deletions src/validators/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl InternalValidator {
validator: validator.clone(),
slots: slots.to_vec(),
data: extra.data.map(|d| d.into_py(py)),
field: extra.field.map(|f| f.to_string()),
field: extra.assignee_field.map(|f| f.to_string()),
strict: extra.strict,
context: extra.context.map(|d| d.into_py(py)),
recursion_guard: recursion_guard.clone(),
Expand All @@ -236,9 +236,10 @@ impl InternalValidator {
{
let extra = Extra {
data: self.data.as_ref().map(|data| data.as_ref(py)),
field: self.field.as_deref(),
assignee_field: self.field.as_deref(),
strict: self.strict,
context: self.context.as_ref().map(|data| data.as_ref(py)),
field_name: None,
};
self.validator
.validate(py, input, &extra, &self.slots, &mut self.recursion_guard)
Expand Down
11 changes: 8 additions & 3 deletions src/validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ impl SchemaValidator {
) -> PyResult<PyObject> {
let extra = Extra {
data: Some(data),
field: Some(field.as_str()),
assignee_field: Some(field.as_str()),
strict,
context,
field_name: None,
};
let r = self
.validator
Expand Down Expand Up @@ -448,8 +449,11 @@ pub struct Extra<'a> {
/// This is used as the `data` kwargs to validator functions, it also represents the current model
/// data when validating assignment
pub data: Option<&'a PyDict>,
/// Represents the fields of the model we are currently validating
/// If there is no model this will be None
pub field_name: Option<&'a str>,
/// The field being assigned to when validating assignment
pub field: Option<&'a str>,
pub assignee_field: Option<&'a str>,
/// whether we're in strict or lax mode
pub strict: Option<bool>,
/// context used in validator functions
Expand All @@ -470,9 +474,10 @@ impl<'a> Extra<'a> {
pub fn as_strict(&self) -> Self {
Self {
data: self.data,
field: self.field,
assignee_field: self.assignee_field,
strict: Some(true),
context: self.context,
field_name: self.field_name,
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/validators/typed_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Validator for TypedDictValidator {
slots: &'data [CombinedValidator],
recursion_guard: &'s mut RecursionGuard,
) -> ValResult<'data, PyObject> {
if let Some(field) = extra.field {
if let Some(field) = extra.assignee_field {
// we're validating assignment, completely different logic
return self.validate_assignment(py, field, input, extra, slots, recursion_guard);
}
Expand All @@ -183,16 +183,14 @@ impl Validator for TypedDictValidator {
false => None,
};

let extra = Extra {
data: Some(output_dict),
field: None,
strict: extra.strict,
context: extra.context,
};

macro_rules! process {
($dict:ident, $get_method:ident, $iter:ty $(,$kwargs:ident)?) => {{
for field in &self.fields {
let extra = Extra {
data: Some(output_dict),
field_name: Some(&field.name),
..*extra
};
let op_key_value = match field.lookup_key.$get_method($dict $(, $kwargs )? ) {
Ok(v) => v,
Err(err) => {
Expand Down Expand Up @@ -348,6 +346,11 @@ impl TypedDictValidator {
where
'data: 's,
{
let extra = Extra {
field_name: Some(field),
assignee_field: None,
..*extra
};
// TODO probably we should set location on errors here
let data = match extra.data {
Some(data) => data,
Expand Down Expand Up @@ -380,12 +383,12 @@ impl TypedDictValidator {
if field.frozen {
Err(ValError::new_with_loc(ErrorType::Frozen, input, field.name.to_string()))
} else {
prepare_result(field.validator.validate(py, input, extra, slots, recursion_guard))
prepare_result(field.validator.validate(py, input, &extra, slots, recursion_guard))
}
} else if self.check_extra && !self.forbid_extra {
// this is the "allow" case of extra_behavior
match self.extra_validator {
Some(ref validator) => prepare_result(validator.validate(py, input, extra, slots, recursion_guard)),
Some(ref validator) => prepare_result(validator.validate(py, input, &extra, slots, recursion_guard)),
None => prepare_tuple(input.to_object(py)),
}
} else {
Expand Down
14 changes: 10 additions & 4 deletions tests/benchmarks/complete_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,33 @@ def wrap_function(input_value, validator, info):
'schema': {
'type': 'function',
'mode': 'before',
'function': append_func,
'function': {'type': 'general', 'function': append_func},
'schema': {'type': 'str'},
}
},
'field_after': {
'schema': {
'type': 'function',
'mode': 'after',
'function': append_func,
'function': {'type': 'general', 'function': append_func},
'schema': {'type': 'str'},
}
},
'field_wrap': {
'schema': {
'type': 'function',
'mode': 'wrap',
'function': wrap_function,
'function': {'type': 'general', 'function': wrap_function},
'schema': {'type': 'str'},
}
},
'field_plain': {'schema': {'type': 'function', 'mode': 'plain', 'function': append_func}},
'field_plain': {
'schema': {
'type': 'function',
'mode': 'plain',
'function': {'type': 'general', 'function': append_func},
}
},
},
}
},
Expand Down
Loading