Skip to content

Commit 24ee390

Browse files
committed
eliminate use of borrowed_from_gil_ref
1 parent 72732b0 commit 24ee390

File tree

11 files changed

+78
-73
lines changed

11 files changed

+78
-73
lines changed

src/errors/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ impl ErrorType {
679679
message_template,
680680
context,
681681
..
682-
} => PydanticCustomError::format_message(message_template, context.as_ref().map(|c| c.as_ref(py))),
682+
} => PydanticCustomError::format_message(message_template, context.as_ref().map(|c| c.attach(py))),
683683
Self::LiteralError { expected, .. } => render!(tmpl, expected),
684684
Self::DateParsing { error, .. } => render!(tmpl, error),
685685
Self::DateFromDatetimeParsing { error, .. } => render!(tmpl, error),

src/errors/value_exception.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl PydanticCustomError {
8989
}
9090

9191
pub fn message(&self, py: Python) -> PyResult<String> {
92-
Self::format_message(&self.message_template, self.context.as_ref().map(|c| c.as_ref(py)))
92+
Self::format_message(&self.message_template, self.context.as_ref().map(|c| c.attach(py)))
9393
}
9494

9595
fn __str__(&self, py: Python) -> PyResult<String> {
@@ -115,14 +115,14 @@ impl PydanticCustomError {
115115
ValError::new(error_type, input)
116116
}
117117

118-
pub fn format_message(message_template: &str, context: Option<&PyDict>) -> PyResult<String> {
118+
pub fn format_message(message_template: &str, context: Option<&Py2<'_, PyDict>>) -> PyResult<String> {
119119
let mut message = message_template.to_string();
120120
if let Some(ctx) = context {
121121
for (key, value) in ctx {
122-
let key: &PyString = key.downcast()?;
122+
let key = key.downcast::<PyString>()?;
123123
if let Ok(py_str) = value.downcast::<PyString>() {
124124
message = message.replace(&format!("{{{}}}", key.to_str()?), py_str.to_str()?);
125-
} else if let Ok(value_int) = extract_i64(Py2::borrowed_from_gil_ref(&value)) {
125+
} else if let Ok(value_int) = extract_i64(&value) {
126126
message = message.replace(&format!("{{{}}}", key.to_str()?), &value_int.to_string());
127127
} else {
128128
// fallback for anything else just in case

src/input/input_abstract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait Input<'a>: fmt::Debug + ToPyObject + AsLocItem + Sized {
5757
false
5858
}
5959

60-
fn input_is_instance(&self, _class: &PyType) -> Option<&PyAny> {
60+
fn input_is_instance(&self, _class: &Py2<'_, PyType>) -> Option<&Py2<'a, PyAny>> {
6161
None
6262
}
6363

src/input/input_python.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ impl<'a> Input<'a> for Py2<'a, PyAny> {
105105
PyAnyMethods::is_none(self)
106106
}
107107

108-
fn input_is_instance(&self, class: &PyType) -> Option<&PyAny> {
109-
if self.is_instance(Py2::borrowed_from_gil_ref(&&**class)).unwrap_or(false) {
110-
Some(self.as_gil_ref())
108+
fn input_is_instance(&self, class: &Py2<'_, PyType>) -> Option<&Py2<'a, PyAny>> {
109+
if self.is_instance(class).unwrap_or(false) {
110+
Some(self)
111111
} else {
112112
None
113113
}

src/validators/dataclass.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,13 @@ impl Validator for DataclassValidator {
524524
}
525525

526526
// same logic as on models
527-
let class = self.class.as_ref(py);
527+
let class = self.class.attach(py);
528528
if let Some(py_input) = input.input_is_instance(class) {
529529
if self.revalidate.should_revalidate(py_input, class) {
530-
let input_dict = self.dataclass_to_dict(Py2::borrowed_from_gil_ref(&py_input))?;
530+
let input_dict = self.dataclass_to_dict(py_input)?;
531531
let val_output = self.validator.validate(py, input_dict.as_any(), state)?;
532532
let dc = create_class(self.class.as_ref(py))?;
533-
self.set_dict_call(py, dc.as_ref(py), val_output, input)?;
533+
self.set_dict_call(py, dc.attach(py), val_output, input)?;
534534
Ok(dc)
535535
} else {
536536
Ok(input.to_object(py))
@@ -547,7 +547,7 @@ impl Validator for DataclassValidator {
547547
let val_output = self.validator.validate(py, input, state)?;
548548
state.floor_exactness(Exactness::Strict);
549549
let dc = create_class(self.class.as_ref(py))?;
550-
self.set_dict_call(py, dc.as_ref(py), val_output, input)?;
550+
self.set_dict_call(py, dc.attach(py), val_output, input)?;
551551
Ok(dc)
552552
}
553553
}
@@ -578,9 +578,9 @@ impl Validator for DataclassValidator {
578578
let value = dc_dict
579579
.get_item(field_name)?
580580
.ok_or_else(|| PyKeyError::new_err(field_name.to_string()))?;
581-
force_setattr(py, obj.as_gil_ref(), field_name, value)?;
581+
force_setattr(py, obj, field_name, value)?;
582582
} else {
583-
force_setattr(py, obj.as_gil_ref(), intern!(py, "__dict__"), dc_dict)?;
583+
force_setattr(py, obj, intern!(py, "__dict__"), dc_dict)?;
584584
}
585585

586586
Ok(obj.to_object(py))
@@ -596,7 +596,7 @@ impl DataclassValidator {
596596
fn validate_init<'s, 'data>(
597597
&'s self,
598598
py: Python<'data>,
599-
self_instance: &'s PyAny,
599+
self_instance: &Py2<'_, PyAny>,
600600
input: &'data impl Input<'data>,
601601
state: &mut ValidationState,
602602
) -> ValResult<PyObject> {
@@ -623,13 +623,13 @@ impl DataclassValidator {
623623
fn set_dict_call<'s, 'data>(
624624
&'s self,
625625
py: Python<'data>,
626-
dc: &PyAny,
626+
dc: &Py2<'_, PyAny>,
627627
val_output: PyObject,
628628
input: &'data impl Input<'data>,
629629
) -> ValResult<()> {
630-
let (dc_dict, post_init_kwargs): (&PyAny, &PyAny) = val_output.extract(py)?;
630+
let (dc_dict, post_init_kwargs): (Py2<'_, PyAny>, Py2<'_, PyAny>) = val_output.extract(py)?;
631631
if self.slots {
632-
let dc_dict: &PyDict = dc_dict.downcast()?;
632+
let dc_dict = dc_dict.downcast::<PyDict>()?;
633633
for (key, value) in dc_dict {
634634
force_setattr(py, dc, key, value)?;
635635
}
@@ -639,11 +639,11 @@ impl DataclassValidator {
639639

640640
if let Some(ref post_init) = self.post_init {
641641
let post_init = post_init.as_ref(py);
642-
let r = if post_init_kwargs.is_none() {
642+
let r = if PyAnyMethods::is_none(&post_init_kwargs) {
643643
dc.call_method0(post_init)
644644
} else {
645645
let args = post_init_kwargs.downcast::<PyTuple>()?;
646-
dc.call_method1(post_init, args)
646+
dc.call_method1(post_init, args.as_gil_ref())
647647
};
648648
r.map_err(|e| convert_err(py, e, input))?;
649649
}

src/validators/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl ValidationInfo {
509509
fn new(py: Python, extra: &Extra, config: &PyObject, field_name: Option<Py<PyString>>) -> Self {
510510
Self {
511511
config: config.clone_ref(py),
512-
context: extra.context.map(Into::into),
512+
context: extra.context.map(|ctx| ctx.clone().into()),
513513
field_name,
514514
data: extra.data.as_ref().map(|data| data.clone().into()),
515515
mode: extra.input_type,

src/validators/generator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ impl InternalValidator {
266266
data: self.data.as_ref().map(|data| data.attach(py).clone()),
267267
strict: self.strict,
268268
from_attributes: self.from_attributes,
269-
context: self.context.as_ref().map(|data| data.as_ref(py)),
270-
self_instance: self.self_instance.as_ref().map(|data| data.as_ref(py)),
269+
context: self.context.as_ref().map(|data| data.attach(py)),
270+
self_instance: self.self_instance.as_ref().map(|data| data.attach(py)),
271271
};
272272
let mut state = ValidationState::new(extra, &mut self.recursion_guard);
273273
state.exactness = self.exactness;
@@ -300,8 +300,8 @@ impl InternalValidator {
300300
data: self.data.as_ref().map(|data| data.attach(py).clone()),
301301
strict: self.strict,
302302
from_attributes: self.from_attributes,
303-
context: self.context.as_ref().map(|data| data.as_ref(py)),
304-
self_instance: self.self_instance.as_ref().map(|data| data.as_ref(py)),
303+
context: self.context.as_ref().map(|data| data.attach(py)),
304+
self_instance: self.self_instance.as_ref().map(|data| data.attach(py)),
305305
};
306306
let mut state = ValidationState::new(extra, &mut self.recursion_guard);
307307
state.exactness = self.exactness;

src/validators/mod.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ impl SchemaValidator {
160160
input: &Py2<'_, PyAny>,
161161
strict: Option<bool>,
162162
from_attributes: Option<bool>,
163-
context: Option<&PyAny>,
164-
self_instance: Option<&PyAny>,
163+
context: Option<&Py2<'_, PyAny>>,
164+
self_instance: Option<&Py2<'_, PyAny>>,
165165
) -> PyResult<PyObject> {
166166
self._validate(
167167
py,
@@ -182,8 +182,8 @@ impl SchemaValidator {
182182
input: &Py2<'_, PyAny>,
183183
strict: Option<bool>,
184184
from_attributes: Option<bool>,
185-
context: Option<&PyAny>,
186-
self_instance: Option<&PyAny>,
185+
context: Option<&Py2<'_, PyAny>>,
186+
self_instance: Option<&Py2<'_, PyAny>>,
187187
) -> PyResult<bool> {
188188
match self._validate(
189189
py,
@@ -208,8 +208,8 @@ impl SchemaValidator {
208208
py: Python,
209209
input: &Py2<'_, PyAny>,
210210
strict: Option<bool>,
211-
context: Option<&PyAny>,
212-
self_instance: Option<&PyAny>,
211+
context: Option<&Py2<'_, PyAny>>,
212+
self_instance: Option<&Py2<'_, PyAny>>,
213213
) -> PyResult<PyObject> {
214214
let r = match json::validate_json_bytes(input) {
215215
Ok(v_match) => self._validate_json(
@@ -231,7 +231,7 @@ impl SchemaValidator {
231231
py: Python,
232232
input: Py2<'_, PyAny>,
233233
strict: Option<bool>,
234-
context: Option<&PyAny>,
234+
context: Option<&Py2<'_, PyAny>>,
235235
) -> PyResult<PyObject> {
236236
let t = InputType::String;
237237
let string_mapping = StringMapping::new_value(&input).map_err(|e| self.prepare_validation_err(py, e, t))?;
@@ -252,7 +252,7 @@ impl SchemaValidator {
252252
field_value: Py2<'_, PyAny>,
253253
strict: Option<bool>,
254254
from_attributes: Option<bool>,
255-
context: Option<&PyAny>,
255+
context: Option<&Py2<'_, PyAny>>,
256256
) -> PyResult<PyObject> {
257257
let extra = Extra {
258258
input_type: InputType::Python,
@@ -271,7 +271,12 @@ impl SchemaValidator {
271271
}
272272

273273
#[pyo3(signature = (*, strict=None, context=None))]
274-
pub fn get_default_value(&self, py: Python, strict: Option<bool>, context: Option<&PyAny>) -> PyResult<PyObject> {
274+
pub fn get_default_value(
275+
&self,
276+
py: Python,
277+
strict: Option<bool>,
278+
context: Option<&Py2<'_, PyAny>>,
279+
) -> PyResult<PyObject> {
275280
let extra = Extra {
276281
input_type: InputType::Python,
277282
data: None,
@@ -320,8 +325,8 @@ impl SchemaValidator {
320325
input_type: InputType,
321326
strict: Option<bool>,
322327
from_attributes: Option<bool>,
323-
context: Option<&'data PyAny>,
324-
self_instance: Option<&PyAny>,
328+
context: Option<&Py2<'data, PyAny>>,
329+
self_instance: Option<&Py2<'_, PyAny>>,
325330
) -> ValResult<PyObject>
326331
where
327332
's: 'data,
@@ -340,8 +345,8 @@ impl SchemaValidator {
340345
input: &Py2<'_, PyAny>,
341346
json_data: &[u8],
342347
strict: Option<bool>,
343-
context: Option<&PyAny>,
344-
self_instance: Option<&PyAny>,
348+
context: Option<&Py2<'_, PyAny>>,
349+
self_instance: Option<&Py2<'_, PyAny>>,
345350
) -> ValResult<PyObject> {
346351
let json_value =
347352
jiter::JsonValue::parse(json_data, true).map_err(|e| json::map_json_err(input, e, json_data))?;
@@ -570,17 +575,17 @@ pub struct Extra<'a> {
570575
/// Validation time setting of `from_attributes`
571576
pub from_attributes: Option<bool>,
572577
/// context used in validator functions
573-
pub context: Option<&'a PyAny>,
578+
pub context: Option<&'a Py2<'a, PyAny>>,
574579
/// This is an instance of the model or dataclass being validated, when validation is performed from `__init__`
575-
self_instance: Option<&'a PyAny>,
580+
self_instance: Option<&'a Py2<'a, PyAny>>,
576581
}
577582

578583
impl<'a> Extra<'a> {
579584
pub fn new(
580585
strict: Option<bool>,
581586
from_attributes: Option<bool>,
582-
context: Option<&'a PyAny>,
583-
self_instance: Option<&'a PyAny>,
587+
context: Option<&'a Py2<'a, PyAny>>,
588+
self_instance: Option<&'a Py2<'a, PyAny>>,
584589
input_type: InputType,
585590
) -> Self {
586591
Extra {

0 commit comments

Comments
 (0)