Skip to content

Commit b7d36df

Browse files
committed
rename arg_kwargs.rs, more tests
1 parent d1ae0fb commit b7d36df

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

src/args_kwargs.rs renamed to src/argument_markers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl ArgsKwargs {
6161
}
6262
}
6363

64-
pub(crate) const VALIDATED_DATA_KEY: &str = "__validated_data__";
64+
pub(crate) const VALIDATED_DATA_KEY: &str = "validated_data";
6565

6666
#[pyclass(module = "pydantic_core._pydantic_core", frozen, get_all, freelist = 100)]
6767
#[derive(Debug, Clone)]

src/input/input_abstract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use pyo3::prelude::*;
44
use pyo3::types::{PyString, PyType};
55

6-
use crate::args_kwargs::ValidatedData;
6+
use crate::argument_markers::ValidatedData;
77
use crate::errors::{InputValue, LocItem, ValResult};
88
use crate::{PyMultiHostUrl, PyUrl};
99

src/input/input_python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use pyo3::types::{
1111
use pyo3::types::{PyDictItems, PyDictKeys, PyDictValues};
1212
use pyo3::{ffi, intern, AsPyPointer, PyTypeInfo};
1313

14-
use crate::args_kwargs::{ValidatedData, VALIDATED_DATA_KEY};
14+
use crate::argument_markers::{ValidatedData, VALIDATED_DATA_KEY};
1515
use crate::build_tools::safe_repr;
1616
use crate::errors::{ErrorType, InputValue, LocItem, ValError, ValResult};
1717
use crate::{ArgsKwargs, PyMultiHostUrl, PyUrl};

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use pyo3::prelude::*;
99
#[global_allocator]
1010
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
1111

12-
mod args_kwargs;
12+
mod argument_markers;
1313
mod build_context;
1414
mod build_tools;
1515
mod errors;
@@ -23,7 +23,7 @@ mod validators;
2323

2424
// required for benchmarks
2525
pub use self::url::{PyMultiHostUrl, PyUrl};
26-
pub use args_kwargs::ArgsKwargs;
26+
pub use argument_markers::ArgsKwargs;
2727
pub use build_tools::SchemaError;
2828
pub use errors::{list_all_errors, PydanticCustomError, PydanticKnownError, PydanticOmit, ValidationError};
2929
pub use serializers::{

src/validators/model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use pyo3::prelude::*;
77
use pyo3::types::{PyDict, PySet, PyString, PyTuple, PyType};
88
use pyo3::{ffi, intern};
99

10-
use crate::args_kwargs::{ValidatedData, VALIDATED_DATA_KEY};
10+
use crate::argument_markers::{ValidatedData, VALIDATED_DATA_KEY};
1111
use crate::build_tools::{py_err, schema_or_config_same, SchemaDict};
1212
use crate::errors::{ErrorType, ValError, ValResult};
1313
use crate::input::{py_error_on_minusone, Input};

tests/validators/test_model.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ class Model:
11701170
__slots__ = '__dict__', '__pydantic_fields_set__'
11711171

11721172
def __init__(self, **kwargs):
1173-
validated_data = kwargs['__validated_data__']
1173+
validated_data = kwargs['validated_data']
11741174
self.a = validated_data.model_dict['a']
11751175
self.b = validated_data.model_dict['b']
11761176
self.__pydantic_fields_set__ = validated_data.fields_set
@@ -1196,4 +1196,67 @@ def __init__(self, **kwargs):
11961196
assert m.a == 1
11971197
assert m.b == 2
11981198
assert m.__pydantic_fields_set__ == {'b'}
1199-
assert calls == ["{'__validated_data__': ValidatedData(model_dict={'a': 1, 'b': 2}, fields_set={'b'})}"]
1199+
assert calls == ["{'validated_data': ValidatedData(model_dict={'a': 1, 'b': 2}, fields_set={'b'})}"]
1200+
1201+
1202+
def test_custom_init_nested():
1203+
calls = []
1204+
1205+
class ModelInner:
1206+
__slots__ = '__dict__', '__pydantic_fields_set__'
1207+
a: int
1208+
b: int
1209+
1210+
def __init__(self, **data):
1211+
calls.append(f'inner: {data!r}')
1212+
self.__pydantic_validator__.validate_python(data, self_instance=self)
1213+
1214+
inner_schema = core_schema.model_schema(
1215+
ModelInner,
1216+
core_schema.typed_dict_schema(
1217+
{
1218+
'a': core_schema.typed_dict_field(core_schema.with_default_schema(core_schema.int_schema(), default=1)),
1219+
'b': core_schema.typed_dict_field(core_schema.int_schema()),
1220+
},
1221+
return_fields_set=True,
1222+
),
1223+
custom_init=True,
1224+
)
1225+
ModelInner.__pydantic_validator__ = SchemaValidator(inner_schema)
1226+
1227+
class ModelOuter:
1228+
__slots__ = '__dict__', '__pydantic_fields_set__'
1229+
a: int
1230+
b: ModelInner
1231+
1232+
def __init__(self, **data):
1233+
calls.append(f'outer: {data!r}')
1234+
self.__pydantic_validator__.validate_python(data, self_instance=self)
1235+
1236+
ModelOuter.__pydantic_validator__ = SchemaValidator(
1237+
core_schema.model_schema(
1238+
ModelOuter,
1239+
core_schema.typed_dict_schema(
1240+
{
1241+
'a': core_schema.typed_dict_field(
1242+
core_schema.with_default_schema(core_schema.int_schema(), default=1)
1243+
),
1244+
'b': core_schema.typed_dict_field(inner_schema),
1245+
},
1246+
return_fields_set=True,
1247+
),
1248+
custom_init=True,
1249+
)
1250+
)
1251+
1252+
m = ModelOuter(a=2, b={'b': 3})
1253+
assert m.__pydantic_fields_set__ == {'a', 'b'}
1254+
assert m.a == 2
1255+
assert isinstance(m.b, ModelInner)
1256+
assert m.b.a == 1
1257+
assert m.b.b == 3
1258+
# insert_assert(calls)
1259+
assert calls == [
1260+
"outer: {'a': 2, 'b': {'b': 3}}",
1261+
"inner: {'validated_data': ValidatedData(model_dict={'a': 1, 'b': 3}, fields_set={'b'})}",
1262+
]

0 commit comments

Comments
 (0)