Skip to content

Commit d7650d1

Browse files
authored
Move from _bound suffixed APIs as part of PyO3 0.23 update (#1577)
1 parent 98bc1e2 commit d7650d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+142
-154
lines changed

src/build_tools.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl SchemaError {
9090
ValidationError::new(line_errors, "Schema".to_object(py), InputType::Python, false);
9191
let schema_error = SchemaError(SchemaErrorEnum::ValidationError(validation_error));
9292
match Py::new(py, schema_error) {
93-
Ok(err) => PyErr::from_value_bound(err.into_bound(py).into_any()),
93+
Ok(err) => PyErr::from_value(err.into_bound(py).into_any()),
9494
Err(err) => err,
9595
}
9696
}
@@ -124,7 +124,7 @@ impl SchemaError {
124124

125125
fn errors(&self, py: Python) -> PyResult<Py<PyList>> {
126126
match &self.0 {
127-
SchemaErrorEnum::Message(_) => Ok(PyList::empty_bound(py).unbind()),
127+
SchemaErrorEnum::Message(_) => Ok(PyList::empty(py).unbind()),
128128
SchemaErrorEnum::ValidationError(error) => error.errors(py, false, false, true),
129129
}
130130
}

src/errors/location.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::lookup_key::{LookupPath, PathItem};
1212

1313
/// Used to store individual items of the error location, e.g. a string for key/field names
1414
/// or a number for array indices.
15-
#[derive(Clone, Eq, PartialEq)]
15+
#[derive(Clone, Eq, PartialEq, IntoPyObjectRef)]
1616
#[cfg_attr(debug_assertions, derive(Debug))]
1717
pub enum LocItem {
1818
/// string type key, used to identify items from a dict or anything that implements `__getitem__`
@@ -133,9 +133,9 @@ static EMPTY_TUPLE: GILOnceCell<PyObject> = GILOnceCell::new();
133133
impl ToPyObject for Location {
134134
fn to_object(&self, py: Python<'_>) -> PyObject {
135135
match self {
136-
Self::List(loc) => PyTuple::new_bound(py, loc.iter().rev()).to_object(py),
136+
Self::List(loc) => PyTuple::new(py, loc.iter().rev()).unwrap().to_object(py),
137137
Self::Empty => EMPTY_TUPLE
138-
.get_or_init(py, || PyTuple::empty_bound(py).to_object(py))
138+
.get_or_init(py, || PyTuple::empty(py).to_object(py))
139139
.clone_ref(py),
140140
}
141141
}

src/errors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::validation_exception::ValidationError;
1313
pub use self::value_exception::{PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticUseDefault};
1414

1515
pub fn py_err_string(py: Python, err: PyErr) -> String {
16-
let value = err.value_bound(py);
16+
let value = err.value(py);
1717
match value.get_type().qualname() {
1818
Ok(type_name) => match value.str() {
1919
Ok(py_str) => {

src/errors/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn list_all_errors(py: Python) -> PyResult<Bound<'_, PyList>> {
2222
let mut errors: Vec<Bound<'_, PyDict>> = Vec::with_capacity(100);
2323
for error_type in ErrorType::iter() {
2424
if !matches!(error_type, ErrorType::CustomError { .. }) {
25-
let d = PyDict::new_bound(py);
25+
let d = PyDict::new(py);
2626
d.set_item("type", error_type.to_string())?;
2727
let message_template_python = error_type.message_template_python();
2828
d.set_item("message_template_python", message_template_python)?;
@@ -39,7 +39,7 @@ pub fn list_all_errors(py: Python) -> PyResult<Bound<'_, PyList>> {
3939
errors.push(d);
4040
}
4141
}
42-
Ok(PyList::new_bound(py, errors))
42+
PyList::new(py, errors)
4343
}
4444

4545
fn field_from_context<'py, T: FromPyObject<'py>>(
@@ -745,7 +745,7 @@ impl ErrorType {
745745
}
746746

747747
pub fn py_dict(&self, py: Python) -> PyResult<Option<Py<PyDict>>> {
748-
let dict = PyDict::new_bound(py);
748+
let dict = PyDict::new(py);
749749
let custom_ctx_used = self.py_dict_update_ctx(py, &dict)?;
750750

751751
if let Self::CustomError { .. } = self {

src/errors/validation_exception.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::{Display, Write};
33
use std::str::from_utf8;
44

55
use pyo3::exceptions::{PyKeyError, PyTypeError, PyValueError};
6-
use pyo3::ffi;
6+
use pyo3::ffi::{self, c_str};
77
use pyo3::intern;
88
use pyo3::prelude::*;
99
use pyo3::sync::GILOnceCell;
@@ -73,7 +73,7 @@ impl ValidationError {
7373
return cause_problem;
7474
}
7575
}
76-
PyErr::from_value_bound(err.into_bound(py).into_any())
76+
PyErr::from_value(err.into_bound(py).into_any())
7777
}
7878
Err(err) => err,
7979
}
@@ -145,7 +145,7 @@ impl ValidationError {
145145
use pyo3::exceptions::PyUserWarning;
146146

147147
let wrapped = PyUserWarning::new_err((note,));
148-
wrapped.set_cause(py, Some(PyErr::from_value_bound(err.clone_ref(py).into_bound(py))));
148+
wrapped.set_cause(py, Some(PyErr::from_value(err.clone_ref(py).into_bound(py))));
149149
user_py_errs.push(wrapped);
150150
}
151151
}
@@ -167,7 +167,7 @@ impl ValidationError {
167167
#[cfg(not(Py_3_11))]
168168
let cause = {
169169
use pyo3::exceptions::PyImportError;
170-
match py.import_bound("exceptiongroup") {
170+
match py.import("exceptiongroup") {
171171
Ok(py_mod) => match py_mod.getattr("ExceptionGroup") {
172172
Ok(group_cls) => match group_cls.call1((title, user_py_errs)) {
173173
Ok(group_instance) => Some(group_instance.into_py(py)),
@@ -202,10 +202,10 @@ fn include_url_env(py: Python) -> bool {
202202
match std::env::var_os("PYDANTIC_ERRORS_OMIT_URL") {
203203
Some(val) => {
204204
// We don't care whether warning succeeded or not, hence the assignment
205-
let _ = PyErr::warn_bound(
205+
let _ = PyErr::warn(
206206
py,
207-
&py.get_type_bound::<pyo3::exceptions::PyDeprecationWarning>(),
208-
"PYDANTIC_ERRORS_OMIT_URL is deprecated, use PYDANTIC_ERRORS_INCLUDE_URL instead",
207+
&py.get_type::<pyo3::exceptions::PyDeprecationWarning>(),
208+
c_str!("PYDANTIC_ERRORS_OMIT_URL is deprecated, use PYDANTIC_ERRORS_INCLUDE_URL instead"),
209209
1,
210210
);
211211
// If OMIT_URL exists but is empty, we include the URL:
@@ -298,7 +298,7 @@ impl ValidationError {
298298
) -> PyResult<Py<PyList>> {
299299
let url_prefix = get_url_prefix(py, include_url);
300300
let mut iteration_error = None;
301-
let list = PyList::new_bound(
301+
let list = PyList::new(
302302
py,
303303
// PyList::new takes ExactSizeIterator, so if an error occurs during iteration we
304304
// fill the list with None before returning the error; the list will then be thrown
@@ -313,7 +313,7 @@ impl ValidationError {
313313
py.None()
314314
})
315315
}),
316-
);
316+
)?;
317317
if let Some(err) = iteration_error {
318318
Err(err)
319319
} else {
@@ -368,7 +368,7 @@ impl ValidationError {
368368
}
369369
};
370370
let s = from_utf8(&bytes).map_err(json_py_err)?;
371-
Ok(PyString::new_bound(py, s))
371+
Ok(PyString::new(py, s))
372372
}
373373

374374
fn __repr__(&self, py: Python) -> String {
@@ -489,7 +489,7 @@ impl PyLineError {
489489
input_type: InputType,
490490
include_input: bool,
491491
) -> PyResult<PyObject> {
492-
let dict = PyDict::new_bound(py);
492+
let dict = PyDict::new(py);
493493
dict.set_item("type", self.error_type.type_string())?;
494494
dict.set_item("loc", self.location.to_object(py))?;
495495
dict.set_item("msg", self.error_type.render_message(py, input_type)?)?;

src/input/datetime.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl EitherDate<'_> {
5656
pub fn try_into_py(self, py: Python<'_>) -> PyResult<PyObject> {
5757
let date = match self {
5858
Self::Py(date) => Ok(date),
59-
Self::Raw(date) => PyDate::new_bound(py, date.year.into(), date.month, date.day),
59+
Self::Raw(date) => PyDate::new(py, date.year.into(), date.month, date.day),
6060
}?;
6161
Ok(date.into_py(py))
6262
}
@@ -165,7 +165,7 @@ pub fn pytimedelta_subclass_as_duration(py_timedelta: &Bound<'_, PyDelta>) -> Py
165165

166166
pub fn duration_as_pytimedelta<'py>(py: Python<'py>, duration: &Duration) -> PyResult<Bound<'py, PyDelta>> {
167167
let sign = if duration.positive { 1 } else { -1 };
168-
PyDelta::new_bound(
168+
PyDelta::new(
169169
py,
170170
sign * duration.day as i32,
171171
sign * duration.second as i32,
@@ -211,7 +211,7 @@ impl EitherTime<'_> {
211211
pub fn try_into_py(self, py: Python<'_>) -> PyResult<PyObject> {
212212
let time = match self {
213213
Self::Py(time) => Ok(time),
214-
Self::Raw(time) => PyTime::new_bound(
214+
Self::Raw(time) => PyTime::new(
215215
py,
216216
time.hour,
217217
time.minute,
@@ -269,7 +269,7 @@ impl<'a> EitherDateTime<'a> {
269269

270270
pub fn try_into_py(self, py: Python<'a>) -> PyResult<PyObject> {
271271
let dt = match self {
272-
Self::Raw(datetime) => PyDateTime::new_bound(
272+
Self::Raw(datetime) => PyDateTime::new(
273273
py,
274274
datetime.date.year.into(),
275275
datetime.date.month,
@@ -393,7 +393,7 @@ pub fn float_as_datetime<'py>(input: &(impl Input<'py> + ?Sized), timestamp: f64
393393

394394
pub fn date_as_datetime<'py>(date: &Bound<'py, PyDate>) -> PyResult<EitherDateTime<'py>> {
395395
let py = date.py();
396-
let dt = PyDateTime::new_bound(
396+
let dt = PyDateTime::new(
397397
py,
398398
date.getattr(intern!(py, "year"))?.extract()?,
399399
date.getattr(intern!(py, "month"))?.extract()?,
@@ -518,7 +518,7 @@ impl TzInfo {
518518

519519
#[allow(unused_variables)]
520520
fn utcoffset<'py>(&self, py: Python<'py>, dt: &Bound<'_, PyAny>) -> PyResult<Bound<'py, PyDelta>> {
521-
PyDelta::new_bound(py, 0, self.seconds, 0, true)
521+
PyDelta::new(py, 0, self.seconds, 0, true)
522522
}
523523

524524
#[allow(unused_variables)]

src/input/input_json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
5656
fn as_kwargs(&self, py: Python<'py>) -> Option<Bound<'py, PyDict>> {
5757
match self {
5858
JsonValue::Object(object) => {
59-
let dict = PyDict::new_bound(py);
59+
let dict = PyDict::new(py);
6060
for (k, v) in LazyIndexMap::iter(object) {
6161
dict.set_item(k, v.to_object(py)).unwrap();
6262
}
@@ -171,7 +171,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
171171
fn validate_decimal(&self, _strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> {
172172
match self {
173173
JsonValue::Float(f) => {
174-
create_decimal(&PyString::new_bound(py, &f.to_string()), self).map(ValidationMatch::strict)
174+
create_decimal(&PyString::new(py, &f.to_string()), self).map(ValidationMatch::strict)
175175
}
176176
JsonValue::Str(..) | JsonValue::Int(..) | JsonValue::BigInt(..) => {
177177
create_decimal(self.to_object(py).bind(py), self).map(ValidationMatch::strict)
@@ -324,7 +324,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
324324
fn validate_complex(&self, strict: bool, py: Python<'py>) -> ValResult<ValidationMatch<EitherComplex<'py>>> {
325325
match self {
326326
JsonValue::Str(s) => Ok(ValidationMatch::strict(EitherComplex::Py(string_to_complex(
327-
&PyString::new_bound(py, s),
327+
&PyString::new(py, s),
328328
self,
329329
)?))),
330330
JsonValue::Float(f) => {

src/input/input_python.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
8787
Some(self)
8888
}
8989

90-
fn as_kwargs(&self, py: Python<'py>) -> Option<Bound<'py, PyDict>> {
91-
self.downcast::<PyDict>()
92-
.ok()
93-
.map(|dict| dict.to_owned().unbind().into_bound(py))
90+
fn as_kwargs(&self, _py: Python<'py>) -> Option<Bound<'py, PyDict>> {
91+
self.downcast::<PyDict>().ok().map(Bound::to_owned)
9492
}
9593

9694
type Arguments<'a>
@@ -620,7 +618,7 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
620618
if strict {
621619
return Err(ValError::new(
622620
ErrorType::IsInstanceOf {
623-
class: PyComplex::type_object_bound(py)
621+
class: PyComplex::type_object(py)
624622
.qualname()
625623
.and_then(|name| name.extract())
626624
.unwrap_or_else(|_| "complex".to_owned()),

src/input/return_enums.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ impl EitherBytes<'_, '_> {
548548
impl IntoPy<PyObject> for EitherBytes<'_, '_> {
549549
fn into_py(self, py: Python<'_>) -> PyObject {
550550
match self {
551-
EitherBytes::Cow(bytes) => PyBytes::new_bound(py, &bytes).into_py(py),
551+
EitherBytes::Cow(bytes) => PyBytes::new(py, &bytes).into_py(py),
552552
EitherBytes::Py(py_bytes) => py_bytes.into_py(py),
553553
}
554554
}
@@ -755,7 +755,7 @@ pub enum EitherComplex<'a> {
755755
impl IntoPy<PyObject> for EitherComplex<'_> {
756756
fn into_py(self, py: Python<'_>) -> PyObject {
757757
match self {
758-
Self::Complex(c) => PyComplex::from_doubles_bound(py, c[0], c[1]).into_py(py),
758+
Self::Complex(c) => PyComplex::from_doubles(py, c[0], c[1]).into_py(py),
759759
Self::Py(c) => c.into_py(py),
760760
}
761761
}

src/input/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static ENUM_META_OBJECT: GILOnceCell<Py<PyAny>> = GILOnceCell::new();
1414
pub fn get_enum_meta_object(py: Python) -> &Bound<'_, PyAny> {
1515
ENUM_META_OBJECT
1616
.get_or_init(py, || {
17-
py.import_bound(intern!(py, "enum"))
17+
py.import(intern!(py, "enum"))
1818
.and_then(|enum_module| enum_module.getattr(intern!(py, "EnumMeta")))
1919
.unwrap()
2020
.into()

0 commit comments

Comments
 (0)