-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathdataclass.rs
669 lines (600 loc) · 26.8 KB
/
dataclass.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
use pyo3::exceptions::PyKeyError;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList, PyString, PyTuple, PyType};
use ahash::AHashSet;
use crate::build_tools::py_schema_err;
use crate::build_tools::{is_strict, schema_or_config_same, ExtraBehavior};
use crate::errors::{ErrorType, ErrorTypeDefaults, ValError, ValLineError, ValResult};
use crate::input::{
input_as_python_instance, Arguments, BorrowInput, Input, InputType, KeywordArgs, PositionalArgs, ValidationMatch,
};
use crate::lookup_key::LookupKey;
use crate::tools::SchemaDict;
use crate::validators::function::convert_err;
use super::model::{create_class, force_setattr, Revalidate};
use super::validation_state::Exactness;
use super::{build_validator, BuildValidator, CombinedValidator, DefinitionsBuilder, ValidationState, Validator};
#[derive(Debug)]
struct Field {
kw_only: bool,
name: String,
py_name: Py<PyString>,
init: bool,
init_only: bool,
lookup_key: LookupKey,
validator: CombinedValidator,
frozen: bool,
}
#[derive(Debug)]
pub struct DataclassArgsValidator {
fields: Vec<Field>,
positional_count: usize,
init_only_count: Option<usize>,
dataclass_name: String,
validator_name: String,
extra_behavior: ExtraBehavior,
extras_validator: Option<Box<CombinedValidator>>,
loc_by_alias: bool,
}
impl BuildValidator for DataclassArgsValidator {
const EXPECTED_TYPE: &'static str = "dataclass-args";
fn build(
schema: &Bound<'_, PyDict>,
config: Option<&Bound<'_, PyDict>>,
definitions: &mut DefinitionsBuilder<CombinedValidator>,
) -> PyResult<CombinedValidator> {
let py = schema.py();
let populate_by_name = schema_or_config_same(schema, config, intern!(py, "populate_by_name"))?.unwrap_or(false);
let extra_behavior = ExtraBehavior::from_schema_or_config(py, schema, config, ExtraBehavior::Ignore)?;
let extras_validator = match (schema.get_item(intern!(py, "extras_schema"))?, &extra_behavior) {
(Some(v), ExtraBehavior::Allow) => Some(Box::new(build_validator(&v, config, definitions)?)),
(Some(_), _) => return py_schema_err!("extras_schema can only be used if extra_behavior=allow"),
(_, _) => None,
};
let fields_schema: Bound<'_, PyList> = schema.get_as_req(intern!(py, "fields"))?;
let mut fields: Vec<Field> = Vec::with_capacity(fields_schema.len());
let mut positional_count = 0;
for field in fields_schema {
let field = field.downcast::<PyDict>()?;
let py_name: Bound<'_, PyString> = field.get_as_req(intern!(py, "name"))?;
let name: String = py_name.extract()?;
let lookup_key = match field.get_item(intern!(py, "validation_alias"))? {
Some(alias) => {
let alt_alias = if populate_by_name { Some(name.as_str()) } else { None };
LookupKey::from_py(py, &alias, alt_alias)?
}
None => LookupKey::from_string(py, &name),
};
let schema = field.get_as_req(intern!(py, "schema"))?;
let validator = match build_validator(&schema, config, definitions) {
Ok(v) => v,
Err(err) => return py_schema_err!("Field '{}':\n {}", name, err),
};
if let CombinedValidator::WithDefault(ref v) = validator {
if v.omit_on_error() {
return py_schema_err!("Field `{}`: omit_on_error cannot be used with arguments", name);
}
}
let kw_only = field.get_as(intern!(py, "kw_only"))?.unwrap_or(true);
if !kw_only {
positional_count += 1;
}
fields.push(Field {
kw_only,
name,
py_name: py_name.into(),
lookup_key,
validator,
init: field.get_as(intern!(py, "init"))?.unwrap_or(true),
init_only: field.get_as(intern!(py, "init_only"))?.unwrap_or(false),
frozen: field.get_as::<bool>(intern!(py, "frozen"))?.unwrap_or(false),
});
}
let init_only_count = if schema.get_as(intern!(py, "collect_init_only"))?.unwrap_or(false) {
Some(fields.iter().filter(|f| f.init_only).count())
} else {
None
};
let dataclass_name: String = schema.get_as_req(intern!(py, "dataclass_name"))?;
let validator_name = format!("dataclass-args[{dataclass_name}]");
Ok(Self {
fields,
positional_count,
init_only_count,
dataclass_name,
validator_name,
extra_behavior,
extras_validator,
loc_by_alias: config.get_as(intern!(py, "loc_by_alias"))?.unwrap_or(true),
}
.into())
}
}
impl_py_gc_traverse!(Field { validator });
impl_py_gc_traverse!(DataclassArgsValidator { fields });
impl Validator for DataclassArgsValidator {
fn validate<'py>(
&self,
py: Python<'py>,
input: &(impl Input<'py> + ?Sized),
state: &mut ValidationState<'_, 'py>,
) -> ValResult<PyObject> {
// this validator does not yet support partial validation, disable it to avoid incorrect results
state.allow_partial = false;
let args = input.validate_dataclass_args(&self.dataclass_name)?;
let output_dict = PyDict::new_bound(py);
let mut init_only_args = self.init_only_count.map(Vec::with_capacity);
let mut errors: Vec<ValLineError> = Vec::new();
let mut used_keys: AHashSet<&str> = AHashSet::with_capacity(self.fields.len());
let state = &mut state.rebind_extra(|extra| extra.data = Some(output_dict.clone()));
let mut fields_set_count: usize = 0;
macro_rules! set_item {
($field:ident, $value:expr) => {{
let py_name = $field.py_name.bind(py);
if $field.init_only {
if let Some(ref mut init_only_args) = init_only_args {
init_only_args.push($value);
}
} else {
output_dict.set_item(py_name, $value)?;
}
}};
}
// go through fields getting the value from args or kwargs and validating it
for (index, field) in self.fields.iter().enumerate() {
if !field.init {
match field.validator.default_value(py, Some(field.name.as_str()), state) {
Ok(Some(value)) => {
// Default value exists, and passed validation if required
set_item!(field, value);
fields_set_count += 1;
}
Ok(None) | Err(ValError::Omit) => continue,
// Note: this will always use the field name even if there is an alias
// However, we don't mind so much because this error can only happen if the
// default value fails validation, which is arguably a developer error.
// We could try to "fix" this in the future if desired.
Err(ValError::LineErrors(line_errors)) => errors.extend(line_errors),
Err(err) => return Err(err),
};
continue;
};
let mut pos_value = None;
if let Some(args) = args.args() {
if !field.kw_only {
pos_value = args.get_item(index);
}
}
let mut kw_value = None;
if let Some(kwargs) = args.kwargs() {
if let Some((lookup_path, value)) = kwargs.get_item(&field.lookup_key)? {
used_keys.insert(lookup_path.first_key());
kw_value = Some((lookup_path, value));
}
}
let kw_value = kw_value.as_ref().map(|(path, value)| (path, value.borrow_input()));
match (pos_value, kw_value) {
// found both positional and keyword arguments, error
(Some(_), Some((_, kw_value))) => {
errors.push(ValLineError::new_with_loc(
ErrorTypeDefaults::MultipleArgumentValues,
kw_value,
field.name.clone(),
));
}
// found a positional argument, validate it
(Some(pos_value), None) => match field.validator.validate(py, pos_value.borrow_input(), state) {
Ok(value) => {
set_item!(field, value);
fields_set_count += 1;
}
Err(ValError::LineErrors(line_errors)) => {
errors.extend(line_errors.into_iter().map(|err| err.with_outer_location(index)));
}
Err(err) => return Err(err),
},
// found a keyword argument, validate it
(None, Some((lookup_path, kw_value))) => match field.validator.validate(py, kw_value, state) {
Ok(value) => {
set_item!(field, value);
fields_set_count += 1;
}
Err(ValError::LineErrors(line_errors)) => {
errors.extend(
line_errors
.into_iter()
.map(|err| lookup_path.apply_error_loc(err, self.loc_by_alias, &field.name)),
);
}
Err(err) => return Err(err),
},
// found neither, check if there is a default value, otherwise error
(None, None) => {
match field.validator.default_value(py, Some(field.name.as_str()), state) {
Ok(Some(value)) => {
// Default value exists, and passed validation if required
set_item!(field, value);
}
Ok(None) => {
// This means there was no default value
errors.push(field.lookup_key.error(
ErrorTypeDefaults::Missing,
input,
self.loc_by_alias,
&field.name,
));
}
Err(ValError::Omit) => continue,
Err(ValError::LineErrors(line_errors)) => {
for err in line_errors {
// Note: this will always use the field name even if there is an alias
// However, we don't mind so much because this error can only happen if the
// default value fails validation, which is arguably a developer error.
// We could try to "fix" this in the future if desired.
errors.push(err);
}
}
Err(err) => return Err(err),
}
}
}
}
// if there are more args than positional_count, add an error for each one
if let Some(args) = args.args() {
let len = args.len();
if len > self.positional_count {
for (index, item) in args.iter().enumerate().skip(self.positional_count) {
errors.push(ValLineError::new_with_loc(
ErrorTypeDefaults::UnexpectedPositionalArgument,
item,
index,
));
}
}
}
// if there are kwargs check any that haven't been processed yet
if let Some(kwargs) = args.kwargs() {
if kwargs.len() != used_keys.len() {
for result in kwargs.iter() {
let (raw_key, value) = result?;
match raw_key
.borrow_input()
.validate_str(true, false)
.map(ValidationMatch::into_inner)
{
Ok(either_str) => {
if !used_keys.contains(either_str.as_cow()?.as_ref()) {
// Unknown / extra field
match self.extra_behavior {
ExtraBehavior::Forbid => {
errors.push(ValLineError::new_with_loc(
ErrorTypeDefaults::UnexpectedKeywordArgument,
value,
raw_key.clone(),
));
}
ExtraBehavior::Ignore => {}
ExtraBehavior::Allow => {
if let Some(ref validator) = self.extras_validator {
match validator.validate(py, value.borrow_input(), state) {
Ok(value) => {
output_dict.set_item(
either_str.as_py_string(py, state.cache_str()),
value,
)?;
}
Err(ValError::LineErrors(line_errors)) => {
for err in line_errors {
errors.push(err.with_outer_location(raw_key.clone()));
}
}
Err(err) => return Err(err),
}
} else {
output_dict
.set_item(either_str.as_py_string(py, state.cache_str()), value)?;
}
}
}
}
}
Err(ValError::LineErrors(line_errors)) => {
for err in line_errors {
errors.push(
err.with_outer_location(raw_key.clone())
.with_type(ErrorTypeDefaults::InvalidKey),
);
}
}
Err(err) => return Err(err),
};
}
}
}
state.add_fields_set(fields_set_count);
if errors.is_empty() {
if let Some(init_only_args) = init_only_args {
Ok((output_dict, PyTuple::new_bound(py, init_only_args)).to_object(py))
} else {
Ok((output_dict, py.None()).to_object(py))
}
} else {
Err(ValError::LineErrors(errors))
}
}
fn validate_assignment<'py>(
&self,
py: Python<'py>,
obj: &Bound<'py, PyAny>,
field_name: &str,
field_value: &Bound<'py, PyAny>,
state: &mut ValidationState<'_, 'py>,
) -> ValResult<PyObject> {
let dict = obj.downcast::<PyDict>()?;
let ok = |output: PyObject| {
dict.set_item(field_name, output)?;
// The second return value represents `init_only_args`
// which doesn't make much sense in this context but we need to put something there
// so that function validators that sit between DataclassValidator and DataclassArgsValidator
// always get called the same shape of data.
Ok(PyTuple::new_bound(py, vec![dict.to_object(py), py.None()]).into_py(py))
};
if let Some(field) = self.fields.iter().find(|f| f.name == field_name) {
if field.frozen {
return Err(ValError::new_with_loc(
ErrorTypeDefaults::FrozenField,
field_value,
field.name.to_string(),
));
}
// by using dict but removing the field in question, we match V1 behaviour
let data_dict = dict.copy()?;
if let Err(err) = data_dict.del_item(field_name) {
// KeyError is fine here as the field might not be in the dict
if !err.get_type_bound(py).is(&PyType::new_bound::<PyKeyError>(py)) {
return Err(err.into());
}
}
match field.validator.validate(
py,
field_value,
&mut state.rebind_extra(|extra| extra.data = Some(data_dict.clone())),
) {
Ok(output) => ok(output),
Err(ValError::LineErrors(line_errors)) => {
let errors = line_errors
.into_iter()
.map(|e| e.with_outer_location(field_name))
.collect();
Err(ValError::LineErrors(errors))
}
Err(err) => Err(err),
}
} else {
// Handle extra (unknown) field
// We partially use the extra_behavior for initialization / validation
// to determine how to handle assignment
match self.extra_behavior {
// For dataclasses we allow assigning unknown fields
// to match stdlib dataclass behavior
ExtraBehavior::Allow => ok(field_value.to_object(py)),
_ => Err(ValError::new_with_loc(
ErrorType::NoSuchAttribute {
attribute: field_name.to_string(),
context: None,
},
field_value,
field_name.to_string(),
)),
}
}
}
fn get_name(&self) -> &str {
&self.validator_name
}
}
#[derive(Debug)]
pub struct DataclassValidator {
strict: bool,
validator: Box<CombinedValidator>,
class: Py<PyType>,
generic_origin: Option<Py<PyType>>,
fields: Vec<Py<PyString>>,
post_init: Option<Py<PyString>>,
revalidate: Revalidate,
name: String,
frozen: bool,
slots: bool,
}
impl BuildValidator for DataclassValidator {
const EXPECTED_TYPE: &'static str = "dataclass";
fn build(
schema: &Bound<'_, PyDict>,
_config: Option<&Bound<'_, PyDict>>,
definitions: &mut DefinitionsBuilder<CombinedValidator>,
) -> PyResult<CombinedValidator> {
let py = schema.py();
// dataclasses ignore the parent config and always use the config from this dataclasses
let config = schema.get_as(intern!(py, "config"))?;
let config = config.as_ref();
let class = schema.get_as_req::<Bound<'_, PyType>>(intern!(py, "cls"))?;
let generic_origin: Option<Bound<'_, PyType>> = schema.get_as(intern!(py, "generic_origin"))?;
let name = match schema.get_as_req::<String>(intern!(py, "cls_name")) {
Ok(name) => name,
Err(_) => class.getattr(intern!(py, "__name__"))?.extract()?,
};
let sub_schema = schema.get_as_req(intern!(py, "schema"))?;
let validator = build_validator(&sub_schema, config, definitions)?;
let post_init = if schema.get_as::<bool>(intern!(py, "post_init"))?.unwrap_or(false) {
Some(intern!(py, "__post_init__").into_py(py))
} else {
None
};
let fields = schema.get_as_req(intern!(py, "fields"))?;
Ok(Self {
strict: is_strict(schema, config)?,
validator: Box::new(validator),
class: class.into(),
generic_origin: generic_origin.map(std::convert::Into::into),
fields,
post_init,
revalidate: Revalidate::from_str(
schema_or_config_same::<Bound<'_, PyString>>(schema, config, intern!(py, "revalidate_instances"))?
.as_ref()
.map(|s| s.to_str())
.transpose()?,
)?,
name,
frozen: schema.get_as(intern!(py, "frozen"))?.unwrap_or(false),
slots: schema.get_as(intern!(py, "slots"))?.unwrap_or(false),
}
.into())
}
}
impl_py_gc_traverse!(DataclassValidator {
class,
generic_origin,
validator
});
impl Validator for DataclassValidator {
fn validate<'py>(
&self,
py: Python<'py>,
input: &(impl Input<'py> + ?Sized),
state: &mut ValidationState<'_, 'py>,
) -> ValResult<PyObject> {
if let Some(self_instance) = state.extra().self_instance {
// in the case that self_instance is Some, we're calling validation from within `BaseModel.__init__`
return self.validate_init(py, self_instance, input, state);
}
// same logic as on models, see more explicit comment in model.rs
let class = self.class.bind(py);
let generic_origin_class = self.generic_origin.as_ref().map(|go| go.bind(py));
let (py_instance_input, force_revalidate): (Option<&Bound<'_, PyAny>>, bool) =
match input_as_python_instance(input, class) {
Some(x) => (Some(x), false),
None => {
// if the model has a generic origin, we allow input data to be instances of the generic origin rather than the class,
// as cases like isinstance(SomeModel[Int], SomeModel[Any]) fail the isinstance check, but are valid, we just have to enforce
// that the data is revalidated, hence we set force_revalidate to true
if generic_origin_class.is_some() {
match input_as_python_instance(input, generic_origin_class.unwrap()) {
Some(x) => (Some(x), true),
None => (None, false),
}
} else {
(None, false)
}
}
};
if let Some(py_input) = py_instance_input {
if self.revalidate.should_revalidate(py_input, class) || force_revalidate {
let input_dict = self.dataclass_to_dict(py_input)?;
let val_output = self.validator.validate(py, input_dict.as_any(), state)?;
let dc = create_class(self.class.bind(py))?;
self.set_dict_call(py, &dc, val_output, input)?;
Ok(dc.into())
} else {
Ok(input.to_object(py))
}
} else if state.strict_or(self.strict) && state.extra().input_type == InputType::Python {
Err(ValError::new(
ErrorType::DataclassExactType {
class_name: self.get_name().to_string(),
context: None,
},
input,
))
} else {
let val_output = self.validator.validate(py, input, state)?;
state.floor_exactness(Exactness::Strict);
let dc = create_class(self.class.bind(py))?;
self.set_dict_call(py, &dc, val_output, input)?;
Ok(dc.into())
}
}
fn validate_assignment<'py>(
&self,
py: Python<'py>,
obj: &Bound<'py, PyAny>,
field_name: &str,
field_value: &Bound<'py, PyAny>,
state: &mut ValidationState<'_, 'py>,
) -> ValResult<PyObject> {
if self.frozen {
return Err(ValError::new(ErrorTypeDefaults::FrozenInstance, field_value));
}
let new_dict = self.dataclass_to_dict(obj)?;
new_dict.set_item(field_name, field_value)?;
let val_assignment_result =
self.validator
.validate_assignment(py, new_dict.as_any(), field_name, field_value, state)?;
let (dc_dict, _): (Bound<'_, PyDict>, Bound<'_, PyAny>) = val_assignment_result.extract(py)?;
if self.slots {
let value = dc_dict
.get_item(field_name)?
.ok_or_else(|| PyKeyError::new_err(field_name.to_string()))?;
force_setattr(py, obj, field_name, value)?;
} else {
force_setattr(py, obj, intern!(py, "__dict__"), dc_dict)?;
}
Ok(obj.to_object(py))
}
fn get_name(&self) -> &str {
&self.name
}
}
impl DataclassValidator {
/// here we just call the inner validator, then set attributes on `self_instance`
fn validate_init<'py>(
&self,
py: Python<'py>,
self_instance: &Bound<'_, PyAny>,
input: &(impl Input<'py> + ?Sized),
state: &mut ValidationState<'_, 'py>,
) -> ValResult<PyObject> {
// we need to set `self_instance` to None for nested validators as we don't want to operate on the self_instance
// instance anymore
let state = &mut state.rebind_extra(|extra| extra.self_instance = None);
let val_output = self.validator.validate(py, input, state)?;
self.set_dict_call(py, self_instance, val_output, input)?;
Ok(self_instance.into_py(py))
}
fn dataclass_to_dict<'py>(&self, dc: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyDict>> {
let py = dc.py();
let dict = PyDict::new_bound(py);
for field_name in &self.fields {
dict.set_item(field_name, dc.getattr(field_name)?)?;
}
Ok(dict)
}
fn set_dict_call<'py>(
&self,
py: Python<'py>,
dc: &Bound<'_, PyAny>,
val_output: PyObject,
input: &(impl Input<'py> + ?Sized),
) -> ValResult<()> {
let (dc_dict, post_init_kwargs): (Bound<'_, PyAny>, Bound<'_, PyAny>) = val_output.extract(py)?;
if self.slots {
let dc_dict = dc_dict.downcast::<PyDict>()?;
for (key, value) in dc_dict.iter() {
force_setattr(py, dc, key, value)?;
}
} else {
force_setattr(py, dc, intern!(py, "__dict__"), dc_dict)?;
}
if let Some(ref post_init) = self.post_init {
let post_init = post_init.bind(py);
let r = if PyAnyMethods::is_none(&post_init_kwargs) {
dc.call_method0(post_init)
} else {
let args = post_init_kwargs.downcast::<PyTuple>()?;
dc.call_method1(post_init, args)
};
r.map_err(|e| convert_err(py, e, input))?;
}
Ok(())
}
}