Skip to content

Commit 3c50dc4

Browse files
committed
switching to init_self
1 parent 4b0fa2c commit 3c50dc4

File tree

14 files changed

+210
-308
lines changed

14 files changed

+210
-308
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pydantic-core"
3-
version = "0.13.0"
3+
version = "0.13.1"
44
edition = "2021"
55
license = "MIT"
66
homepage = "https://github.com/pydantic/pydantic-core"

pydantic_core/_pydantic_core.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@ class SchemaValidator:
3838
def title(self) -> str: ...
3939
def __init__(self, schema: CoreSchema, config: 'CoreConfig | None' = None) -> None: ...
4040
def validate_python(
41-
self, input: Any, *, strict: 'bool | None' = None, context: Any = None, init_mode: bool = False
41+
self, input: Any, *, strict: 'bool | None' = None, context: Any = None, init_self: 'Any | None' = None
4242
) -> Any: ...
4343
def isinstance_python(
44-
self, input: Any, *, strict: 'bool | None' = None, context: Any = None, init_mode: bool = False
44+
self, input: Any, *, strict: 'bool | None' = None, context: Any = None, init_self: 'Any | None' = None
4545
) -> bool: ...
4646
def validate_json(
4747
self,
4848
input: 'str | bytes | bytearray',
4949
*,
5050
strict: 'bool | None' = None,
5151
context: Any = None,
52-
init_mode: bool = False,
52+
init_self: 'Any | None' = None,
5353
) -> Any: ...
5454
def isinstance_json(
5555
self,
5656
input: 'str | bytes | bytearray',
5757
*,
5858
strict: 'bool | None' = None,
5959
context: Any = None,
60-
init_mode: bool = False,
60+
init_self: 'Any | None' = None,
6161
) -> bool: ...
6262
def validate_assignment(
6363
self, field: str, input: Any, data: 'dict[str, Any]', strict: 'bool | None' = None, context: Any = None

pydantic_core/core_schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,7 +2392,7 @@ class ModelSchema(TypedDict, total=False):
23922392
type: Required[Literal['model']]
23932393
cls: Required[Type[Any]]
23942394
schema: Required[CoreSchema]
2395-
call_after_init: str
2395+
post_init: str
23962396
strict: bool
23972397
config: CoreConfig
23982398
ref: str
@@ -2404,7 +2404,7 @@ def model_schema(
24042404
cls: Type[Any],
24052405
schema: CoreSchema,
24062406
*,
2407-
call_after_init: str | None = None,
2407+
post_init: str | None = None,
24082408
strict: bool | None = None,
24092409
config: CoreConfig | None = None,
24102410
ref: str | None = None,
@@ -2440,7 +2440,7 @@ class MyModel:
24402440
Args:
24412441
cls: The class to use for the model
24422442
schema: The schema to use for the model
2443-
call_after_init: The call after init to use for the model
2443+
post_init: The call after init to use for the model
24442444
strict: Whether the model is strict
24452445
config: The config to use for the model
24462446
ref: See [TODO] for details
@@ -2451,7 +2451,7 @@ class MyModel:
24512451
type='model',
24522452
cls=cls,
24532453
schema=schema,
2454-
call_after_init=call_after_init,
2454+
post_init=post_init,
24552455
strict=strict,
24562456
config=config,
24572457
ref=ref,

src/url.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl PyUrl {
3737
pub fn py_new(py: Python, url: &PyAny) -> PyResult<Self> {
3838
let schema_obj = SCHEMA_DEFINITION_URL
3939
.get_or_init(py, || build_schema_validator(py, "url"))
40-
.validate_python(py, url, None, None, false)?;
40+
.validate_python(py, url, None, None, None)?;
4141
schema_obj.extract(py)
4242
}
4343

@@ -147,7 +147,7 @@ impl PyMultiHostUrl {
147147
pub fn py_new(py: Python, url: &PyAny) -> PyResult<Self> {
148148
let schema_obj = SCHEMA_DEFINITION_MULTI_HOST_URL
149149
.get_or_init(py, || build_schema_validator(py, "multi-host-url"))
150-
.validate_python(py, url, None, None, false)?;
150+
.validate_python(py, url, None, None, None)?;
151151
schema_obj.extract(py)
152152
}
153153

src/validators/dataclass.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::recursion_guard::RecursionGuard;
1212
use crate::validators::function::convert_err;
1313

1414
use super::arguments::{json_get, json_slice, py_get, py_slice};
15-
use super::model::create_class;
15+
use super::model::{create_class, force_setattr};
1616
use super::with_default::get_default;
1717
use super::{build_validator, BuildContext, BuildValidator, CombinedValidator, Extra, Validator};
1818

@@ -350,7 +350,8 @@ impl Validator for DataclassValidator {
350350
let input = input.maybe_subclass_dict(class)?;
351351
let output = self.validator.validate(py, input, extra, slots, recursion_guard)?;
352352
let (dc_dict, post_init_kwargs): (&PyAny, &PyAny) = output.extract(py)?;
353-
let dc = create_class(self.class.as_ref(py), dc_dict, None)?;
353+
let dc = create_class(self.class.as_ref(py))?;
354+
force_setattr(py, dc.as_ref(py), intern!(py, "__dict__"), dc_dict)?;
354355

355356
if let Some(ref post_init) = self.post_init {
356357
let post_init = post_init.as_ref(py);

src/validators/function.rs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,6 @@ impl Validator for FunctionBeforeValidator {
110110
.validate(py, value.into_ref(py), extra, slots, recursion_guard)
111111
}
112112

113-
fn validate_init<'s, 'data>(
114-
&'s self,
115-
py: Python<'data>,
116-
input: &'data impl Input<'data>,
117-
extra: &Extra,
118-
slots: &'data [CombinedValidator],
119-
recursion_guard: &'s mut RecursionGuard,
120-
) -> ValResult<'data, PyObject> {
121-
let info = ValidationInfo::new(py, extra, &self.config);
122-
let value = self
123-
.func
124-
.call1(py, (input.to_object(py), info))
125-
.map_err(|e| convert_err(py, e, input))?;
126-
127-
self.validator
128-
.validate_init(py, value.into_ref(py), extra, slots, recursion_guard)
129-
}
130-
131113
fn get_name(&self) -> &str {
132114
&self.name
133115
}
@@ -166,19 +148,6 @@ impl Validator for FunctionAfterValidator {
166148
self.func.call1(py, (v, info)).map_err(|e| convert_err(py, e, input))
167149
}
168150

169-
fn validate_init<'s, 'data>(
170-
&'s self,
171-
py: Python<'data>,
172-
input: &'data impl Input<'data>,
173-
extra: &Extra,
174-
slots: &'data [CombinedValidator],
175-
recursion_guard: &'s mut RecursionGuard,
176-
) -> ValResult<'data, PyObject> {
177-
let v = self.validator.validate_init(py, input, extra, slots, recursion_guard)?;
178-
let info = ValidationInfo::new(py, extra, &self.config);
179-
self.func.call1(py, (v, info)).map_err(|e| convert_err(py, e, input))
180-
}
181-
182151
fn get_name(&self) -> &str {
183152
&self.name
184153
}
@@ -258,40 +227,7 @@ impl Validator for FunctionWrapValidator {
258227
recursion_guard: &'s mut RecursionGuard,
259228
) -> ValResult<'data, PyObject> {
260229
let call_next_validator = ValidatorCallable {
261-
validator: InternalValidator::new(
262-
py,
263-
"ValidatorCallable",
264-
&self.validator,
265-
slots,
266-
extra,
267-
recursion_guard,
268-
false,
269-
),
270-
};
271-
let info = ValidationInfo::new(py, extra, &self.config);
272-
self.func
273-
.call1(py, (input.to_object(py), call_next_validator, info))
274-
.map_err(|e| convert_err(py, e, input))
275-
}
276-
277-
fn validate_init<'s, 'data>(
278-
&'s self,
279-
py: Python<'data>,
280-
input: &'data impl Input<'data>,
281-
extra: &Extra,
282-
slots: &'data [CombinedValidator],
283-
recursion_guard: &'s mut RecursionGuard,
284-
) -> ValResult<'data, PyObject> {
285-
let call_next_validator = ValidatorCallable {
286-
validator: InternalValidator::new(
287-
py,
288-
"ValidatorCallable",
289-
&self.validator,
290-
slots,
291-
extra,
292-
recursion_guard,
293-
true,
294-
),
230+
validator: InternalValidator::new(py, "ValidatorCallable", &self.validator, slots, extra, recursion_guard),
295231
};
296232
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
297233
self.func

src/validators/generator.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Validator for GeneratorValidator {
5757
let validator = self
5858
.item_validator
5959
.as_ref()
60-
.map(|v| InternalValidator::new(py, "ValidatorIterator", v, slots, extra, recursion_guard, false));
60+
.map(|v| InternalValidator::new(py, "ValidatorIterator", v, slots, extra, recursion_guard));
6161

6262
let v_iterator = ValidatorIterator {
6363
iterator,
@@ -195,8 +195,8 @@ pub struct InternalValidator {
195195
field: Option<String>,
196196
strict: Option<bool>,
197197
context: Option<PyObject>,
198+
init_self: Option<PyObject>,
198199
recursion_guard: RecursionGuard,
199-
init_mode: bool,
200200
}
201201

202202
impl fmt::Debug for InternalValidator {
@@ -213,7 +213,6 @@ impl InternalValidator {
213213
slots: &[CombinedValidator],
214214
extra: &Extra,
215215
recursion_guard: &RecursionGuard,
216-
init_mode: bool,
217216
) -> Self {
218217
Self {
219218
name: name.to_string(),
@@ -223,8 +222,8 @@ impl InternalValidator {
223222
field: extra.assignee_field.map(|f| f.to_string()),
224223
strict: extra.strict,
225224
context: extra.context.map(|d| d.into_py(py)),
225+
init_self: extra.init_self.map(|d| d.into_py(py)),
226226
recursion_guard: recursion_guard.clone(),
227-
init_mode,
228227
}
229228
}
230229

@@ -243,14 +242,10 @@ impl InternalValidator {
243242
strict: self.strict,
244243
context: self.context.as_ref().map(|data| data.as_ref(py)),
245244
field_name: None,
245+
init_self: self.init_self.as_ref().map(|data| data.as_ref(py)),
246246
};
247-
let r = if self.init_mode {
248-
self.validator
249-
.validate_init(py, input, &extra, &self.slots, &mut self.recursion_guard)
250-
} else {
251-
self.validator
252-
.validate(py, input, &extra, &self.slots, &mut self.recursion_guard)
253-
};
254-
r.map_err(|e| ValidationError::from_val_error(py, self.name.to_object(py), e, outer_location))
247+
self.validator
248+
.validate(py, input, &extra, &self.slots, &mut self.recursion_guard)
249+
.map_err(|e| ValidationError::from_val_error(py, self.name.to_object(py), e, outer_location))
255250
}
256251
}

0 commit comments

Comments
 (0)