Skip to content

Commit 4c4fe8a

Browse files
committed
remove custom_init logic
1 parent b7d36df commit 4c4fe8a

File tree

6 files changed

+3
-172
lines changed

6 files changed

+3
-172
lines changed

pydantic_core/core_schema.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,7 +2737,6 @@ class ModelSchema(TypedDict, total=False):
27372737
revalidate_instances: Literal['always', 'never', 'subclass-instances'] # default: 'never'
27382738
strict: bool
27392739
frozen: bool
2740-
custom_init: bool
27412740
config: CoreConfig
27422741
ref: str
27432742
metadata: Any
@@ -2752,7 +2751,6 @@ def model_schema(
27522751
revalidate_instances: Literal['always', 'never', 'subclass-instances'] | None = None,
27532752
strict: bool | None = None,
27542753
frozen: bool | None = None,
2755-
custom_init: bool | None = None,
27562754
config: CoreConfig | None = None,
27572755
ref: str | None = None,
27582756
metadata: Any = None,
@@ -2793,7 +2791,6 @@ class MyModel:
27932791
should re-validate defaults to config.revalidate_instances, else 'never'
27942792
strict: Whether the model is strict
27952793
frozen: Whether the model is frozen
2796-
custom_init: Whether the model has a custom init method
27972794
config: The config to use for the model
27982795
ref: optional unique identifier of the schema, used to reference the schema in other places
27992796
metadata: Any other information you want to include with the schema, not used by pydantic-core
@@ -2807,7 +2804,6 @@ class MyModel:
28072804
revalidate_instances=revalidate_instances,
28082805
strict=strict,
28092806
frozen=frozen,
2810-
custom_init=custom_init,
28112807
config=config,
28122808
ref=ref,
28132809
metadata=metadata,

src/argument_markers.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,3 @@ impl ArgsKwargs {
6060
}
6161
}
6262
}
63-
64-
pub(crate) const VALIDATED_DATA_KEY: &str = "validated_data";
65-
66-
#[pyclass(module = "pydantic_core._pydantic_core", frozen, get_all, freelist = 100)]
67-
#[derive(Debug, Clone)]
68-
pub struct ValidatedData {
69-
pub model_dict: PyObject,
70-
pub fields_set: PyObject,
71-
}
72-
73-
impl ValidatedData {
74-
pub(crate) fn new(model_dict: &PyAny, fields_set: &PyAny) -> Self {
75-
Self {
76-
model_dict: model_dict.to_object(model_dict.py()),
77-
fields_set: fields_set.to_object(model_dict.py()),
78-
}
79-
}
80-
}
81-
82-
#[pymethods]
83-
impl ValidatedData {
84-
fn __repr__(&self, py: Python) -> String {
85-
let model_dict = safe_repr(self.model_dict.as_ref(py));
86-
let fields_set = safe_repr(self.fields_set.as_ref(py));
87-
format!("ValidatedData(model_dict={model_dict}, fields_set={fields_set})")
88-
}
89-
}

src/input/input_abstract.rs

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

6-
use crate::argument_markers::ValidatedData;
76
use crate::errors::{InputValue, LocItem, ValResult};
87
use crate::{PyMultiHostUrl, PyUrl};
98

@@ -45,11 +44,6 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
4544
None
4645
}
4746

48-
#[cfg_attr(has_no_coverage, no_coverage)]
49-
fn validated_data(&self) -> Option<ValidatedData> {
50-
None
51-
}
52-
5347
// input_ prefix to differentiate from the function on PyAny
5448
fn input_is_instance(&self, class: &PyAny, json_mask: u8) -> PyResult<bool>;
5549

src/input/input_python.rs

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

14-
use crate::argument_markers::{ValidatedData, VALIDATED_DATA_KEY};
1514
use crate::build_tools::safe_repr;
1615
use crate::errors::{ErrorType, InputValue, LocItem, ValError, ValResult};
1716
use crate::{ArgsKwargs, PyMultiHostUrl, PyUrl};
@@ -103,16 +102,6 @@ impl<'a> Input<'a> for PyAny {
103102
Some(self.getattr(name))
104103
}
105104

106-
#[cfg_attr(has_no_coverage, no_coverage)]
107-
fn validated_data(&self) -> Option<ValidatedData> {
108-
if let Ok(v) = self.get_item(intern!(self.py(), VALIDATED_DATA_KEY)) {
109-
if let Ok(validated_data) = v.extract::<ValidatedData>() {
110-
return Some(validated_data);
111-
}
112-
}
113-
None
114-
}
115-
116105
fn input_is_instance(&self, class: &PyAny, _json_mask: u8) -> PyResult<bool> {
117106
// See PyO3/pyo3#2694 - we can't use `is_instance` here since it requires PyType,
118107
// and some check objects are not types, this logic is lifted from `is_instance` in PyO3

src/validators/model.rs

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

10-
use crate::argument_markers::{ValidatedData, VALIDATED_DATA_KEY};
1110
use crate::build_tools::{py_err, schema_or_config_same, SchemaDict};
1211
use crate::errors::{ErrorType, ValError, ValResult};
1312
use crate::input::{py_error_on_minusone, Input};
@@ -52,7 +51,6 @@ pub struct ModelValidator {
5251
post_init: Option<Py<PyString>>,
5352
name: String,
5453
frozen: bool,
55-
custom_init: bool,
5654
}
5755

5856
impl BuildValidator for ModelValidator {
@@ -89,7 +87,6 @@ impl BuildValidator for ModelValidator {
8987
// which is not what we want here
9088
name: class.getattr(intern!(py, "__name__"))?.extract()?,
9189
frozen: schema.get_as(intern!(py, "frozen"))?.unwrap_or(false),
92-
custom_init: schema.get_as(intern!(py, "custom_init"))?.unwrap_or(false),
9390
}
9491
.into())
9592
}
@@ -230,18 +227,6 @@ impl ModelValidator {
230227
..*extra
231228
};
232229

233-
if self.custom_init {
234-
if let Some(validated_data) = input.validated_data() {
235-
set_model_attrs(
236-
self_instance,
237-
validated_data.model_dict.as_ref(py),
238-
validated_data.fields_set.as_ref(py),
239-
)?;
240-
// we don't call post_init here, it'll be called by the original validator
241-
return Ok(self_instance.into_py(py));
242-
}
243-
}
244-
245230
let output = self.validator.validate(py, input, &new_extra, slots, recursion_guard)?;
246231
let (model_dict, fields_set): (&PyAny, &PyAny) = output.extract(py)?;
247232
set_model_attrs(self_instance, model_dict, fields_set)?;
@@ -265,16 +250,9 @@ impl ModelValidator {
265250

266251
fn create_class(&self, model_dict: &PyAny, fields_set: &PyAny) -> PyResult<PyObject> {
267252
let py = model_dict.py();
268-
if self.custom_init {
269-
let kwargs = PyDict::new(py);
270-
let vd = ValidatedData::new(model_dict, fields_set);
271-
kwargs.set_item(intern!(py, VALIDATED_DATA_KEY), vd.into_py(py))?;
272-
self.class.call(py, (), Some(kwargs))
273-
} else {
274-
let instance = create_class(self.class.as_ref(py))?;
275-
set_model_attrs(instance.as_ref(py), model_dict, fields_set)?;
276-
Ok(instance)
277-
}
253+
let instance = create_class(self.class.as_ref(py))?;
254+
set_model_attrs(instance.as_ref(py), model_dict, fields_set)?;
255+
Ok(instance)
278256
}
279257
}
280258

tests/validators/test_model.py

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,102 +1161,3 @@ def f(values_or_values_and_fields_set: Any, *args: Any) -> Any:
11611161
assert m.b == 2
11621162
assert m.__pydantic_fields_set__ == {'a', 'b'}
11631163
assert calls == [call1, call2]
1164-
1165-
1166-
def test_custom_init():
1167-
calls = []
1168-
1169-
class Model:
1170-
__slots__ = '__dict__', '__pydantic_fields_set__'
1171-
1172-
def __init__(self, **kwargs):
1173-
validated_data = kwargs['validated_data']
1174-
self.a = validated_data.model_dict['a']
1175-
self.b = validated_data.model_dict['b']
1176-
self.__pydantic_fields_set__ = validated_data.fields_set
1177-
calls.append(repr(kwargs))
1178-
1179-
v = SchemaValidator(
1180-
core_schema.model_schema(
1181-
Model,
1182-
core_schema.typed_dict_schema(
1183-
{
1184-
'a': core_schema.typed_dict_field(
1185-
core_schema.with_default_schema(core_schema.int_schema(), default=1)
1186-
),
1187-
'b': core_schema.typed_dict_field(core_schema.int_schema()),
1188-
},
1189-
return_fields_set=True,
1190-
),
1191-
custom_init=True,
1192-
)
1193-
)
1194-
1195-
m = v.validate_python({'b': 2})
1196-
assert m.a == 1
1197-
assert m.b == 2
1198-
assert m.__pydantic_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)