-
Notifications
You must be signed in to change notification settings - Fork 253
/
python.rs
285 lines (255 loc) · 9.81 KB
/
python.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
use std::str::from_utf8;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyInt, PyList, PyMapping, PyString, PyTuple, PyType};
use crate::errors::{as_internal, err_val_error, ErrorKind, InputValue, LocItem, ValResult};
use super::shared::{float_as_int, int_as_bool, str_as_bool, str_as_int};
use super::traits::{DictInput, Input, ListInput, ToLocItem, ToPy};
impl Input for PyAny {
fn is_none(&self) -> bool {
self.is_none()
}
fn strict_str(&self) -> ValResult<String> {
if let Ok(py_str) = self.cast_as::<PyString>() {
py_str.extract().map_err(as_internal)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::StrType)
}
}
fn lax_str(&self) -> ValResult<String> {
if let Ok(py_str) = self.cast_as::<PyString>() {
py_str.extract().map_err(as_internal)
} else if let Ok(bytes) = self.cast_as::<PyBytes>() {
let str = match from_utf8(bytes.as_bytes()) {
Ok(s) => s.to_string(),
Err(_) => {
return err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::StrUnicode)
}
};
Ok(str)
} else if self.extract::<bool>().is_ok() {
// do this before int and float parsing as `False` is cast to `0` and we don't want False to
// be returned as a string
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::StrType)
} else if let Ok(int) = self.cast_as::<PyInt>() {
let int = i64::extract(int).map_err(as_internal)?;
Ok(int.to_string())
} else if let Ok(float) = f64::extract(self) {
// don't cast_as here so Decimals are covered - internally f64:extract uses PyFloat_AsDouble
Ok(float.to_string())
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::StrType)
}
}
fn strict_bool(&self) -> ValResult<bool> {
if let Ok(bool) = self.extract::<bool>() {
Ok(bool)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::BoolType)
}
}
fn lax_bool(&self) -> ValResult<bool> {
if let Ok(bool) = self.extract::<bool>() {
Ok(bool)
} else if let Some(str) = _maybe_as_string(self, ErrorKind::BoolParsing)? {
str_as_bool(self, &str)
} else if let Ok(int) = self.extract::<i64>() {
int_as_bool(self, int)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::BoolType)
}
}
fn strict_int(&self) -> ValResult<i64> {
// bool check has to come before int check as bools would be cast to ints below
if self.extract::<bool>().is_ok() {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::IntType)
} else if let Ok(int) = self.extract::<i64>() {
Ok(int)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::IntType)
}
}
fn lax_int(&self) -> ValResult<i64> {
if let Ok(int) = self.extract::<i64>() {
Ok(int)
} else if let Some(str) = _maybe_as_string(self, ErrorKind::IntParsing)? {
str_as_int(self, &str)
} else if let Ok(float) = self.lax_float() {
float_as_int(self, float)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::IntType)
}
}
fn strict_float(&self) -> ValResult<f64> {
if self.extract::<bool>().is_ok() {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::FloatType)
} else if let Ok(float) = self.extract::<f64>() {
Ok(float)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::FloatType)
}
}
fn lax_float(&self) -> ValResult<f64> {
if let Ok(int) = self.extract::<f64>() {
Ok(int)
} else if let Some(str) = _maybe_as_string(self, ErrorKind::FloatParsing)? {
match str.parse() {
Ok(i) => Ok(i),
Err(_) => err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::FloatParsing),
}
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::FloatType)
}
}
fn strict_model_check(&self, class: &PyType) -> ValResult<bool> {
self.get_type().eq(class).map_err(as_internal)
}
fn strict_dict<'data>(&'data self) -> ValResult<Box<dyn DictInput<'data> + 'data>> {
if let Ok(dict) = self.cast_as::<PyDict>() {
Ok(Box::new(dict))
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::DictType)
}
}
fn lax_dict<'data>(&'data self, try_instance: bool) -> ValResult<Box<dyn DictInput<'data> + 'data>> {
if let Ok(dict) = self.cast_as::<PyDict>() {
Ok(Box::new(dict))
} else if let Ok(mapping) = self.cast_as::<PyMapping>() {
// this is ugly, but we'd have to do it in `input_iter` anyway
// we could perhaps use an indexmap instead of a python dict?
let dict = match mapping_as_dict(mapping) {
Ok(dict) => dict,
Err(err) => {
return err_val_error!(
input_value = InputValue::InputRef(self),
message = Some(err.to_string()),
kind = ErrorKind::DictFromMapping
)
}
};
Ok(Box::new(dict))
} else if try_instance {
let inner_dict = match instance_as_dict(self) {
Ok(dict) => dict,
Err(err) => {
return err_val_error!(
input_value = InputValue::InputRef(self),
message = Some(err.to_string()),
kind = ErrorKind::DictFromObject
)
}
};
inner_dict.lax_dict(false)
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::DictType)
}
}
fn strict_list<'data>(&'data self) -> ValResult<Box<dyn ListInput + 'data>> {
if let Ok(list) = self.cast_as::<PyList>() {
Ok(Box::new(list))
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::ListType)
}
}
fn lax_list<'data>(&'data self) -> ValResult<Box<dyn ListInput + 'data>> {
if let Ok(list) = self.cast_as::<PyList>() {
Ok(Box::new(list))
// TODO support sets, tuples, frozen set etc. like in pydantic
} else {
err_val_error!(input_value = InputValue::InputRef(self), kind = ErrorKind::ListType)
}
}
}
fn mapping_as_dict(mapping: &PyMapping) -> PyResult<&PyDict> {
let seq = mapping.items()?;
let dict = PyDict::new(mapping.py());
for r in seq.iter()? {
let t: &PyTuple = r?.extract()?;
let k = t.get_item(0)?;
let v = t.get_item(1)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
/// This is equivalent to `GetterDict` in pydantic v1
fn instance_as_dict(instance: &PyAny) -> PyResult<&PyDict> {
let dict = PyDict::new(instance.py());
for k_any in instance.dir() {
let k_str: &str = k_any.extract()?;
if !k_str.starts_with('_') {
let v = instance.getattr(k_any)?;
dict.set_item(k_any, v)?;
}
}
Ok(dict)
}
impl<'data> DictInput<'data> for &'data PyDict {
fn input_iter(&self) -> Box<dyn Iterator<Item = (&'data dyn Input, &'data dyn Input)> + 'data> {
Box::new(self.iter().map(|(k, v)| (k as &dyn Input, v as &dyn Input)))
}
fn input_get(&self, key: &str) -> Option<&'data dyn Input> {
self.get_item(key).map(|item| item as &dyn Input)
}
fn input_len(&self) -> usize {
self.len()
}
}
impl<'data> ListInput<'data> for &'data PyList {
fn input_iter(&self) -> Box<dyn Iterator<Item = &'data dyn Input> + 'data> {
Box::new(self.iter().map(|item| item as &dyn Input))
}
fn input_len(&self) -> usize {
self.len()
}
}
/// Utility for extracting a string from a PyAny, if possible.
fn _maybe_as_string(v: &PyAny, unicode_error: ErrorKind) -> ValResult<Option<String>> {
if let Ok(str) = v.extract::<String>() {
Ok(Some(str))
} else if let Ok(bytes) = v.cast_as::<PyBytes>() {
let str = match from_utf8(bytes.as_bytes()) {
Ok(s) => s.to_string(),
Err(_) => return err_val_error!(input_value = InputValue::InputRef(v), kind = unicode_error),
};
Ok(Some(str))
} else {
Ok(None)
}
}
impl ToPy for PyAny {
#[inline]
fn to_py(&self, py: Python) -> PyObject {
self.into_py(py)
}
}
impl ToPy for &PyDict {
#[inline]
fn to_py(&self, py: Python) -> PyObject {
self.into_py(py)
}
}
impl ToPy for &PyList {
#[inline]
fn to_py(&self, py: Python) -> PyObject {
self.into_py(py)
}
}
impl ToLocItem for PyAny {
fn to_loc(&self) -> LocItem {
if let Ok(key_str) = self.extract::<String>() {
LocItem::S(key_str)
} else if let Ok(key_int) = self.extract::<usize>() {
LocItem::I(key_int)
} else {
// best effort is to use repr
match repr_string(self) {
Ok(s) => LocItem::S(s),
Err(_) => LocItem::S(format!("{:?}", self)),
}
}
}
}
fn repr_string(py_any: &PyAny) -> PyResult<String> {
let repr_result = py_any.repr()?;
let repr: String = repr_result.extract()?;
Ok(repr)
}