Skip to content

Commit c972150

Browse files
committed
update to PyO3 0.21 beta
1 parent 4a533aa commit c972150

32 files changed

+90
-116
lines changed

Cargo.lock

Lines changed: 13 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ include = [
2727
rust-version = "1.70"
2828

2929
[dependencies]
30-
pyo3 = { version = "0.20.3", features = ["generate-import-lib", "num-bigint"] }
30+
pyo3 = { version = "0.21.0-beta.0", features = ["generate-import-lib", "num-bigint", "gil-refs"] }
3131
regex = "1.10.3"
3232
strum = { version = "0.25.0", features = ["derive"] }
3333
strum_macros = "0.26.1"
@@ -44,7 +44,7 @@ base64 = "0.21.7"
4444
num-bigint = "0.4.4"
4545
python3-dll-a = "0.2.7"
4646
uuid = "1.7.0"
47-
jiter = {version = "0.0.6", features = ["python"]}
47+
jiter = {version = "0.0.7", features = ["python"]}
4848

4949
[lib]
5050
name = "_pydantic_core"
@@ -71,12 +71,12 @@ debug = true
7171
strip = false
7272

7373
[dev-dependencies]
74-
pyo3 = { version = "0.20.3", features = ["auto-initialize"] }
74+
pyo3 = { version = "0.21.0-beta.0", features = ["auto-initialize"] }
7575

7676
[build-dependencies]
7777
version_check = "0.9.4"
7878
# used where logic has to be version/distribution specific, e.g. pypy
79-
pyo3-build-config = { version = "0.20.2" }
79+
pyo3-build-config = { version = "0.21.0-beta.0" }
8080

8181
[lints.clippy]
8282
dbg_macro = "warn"

src/build_tools.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::ValidationError;
1414
pub fn schema_or_config<'py, T>(
1515
schema: &'py PyDict,
1616
config: Option<&'py PyDict>,
17-
schema_key: &PyString,
18-
config_key: &PyString,
17+
schema_key: &Bound<'py, PyString>,
18+
config_key: &Bound<'py, PyString>,
1919
) -> PyResult<Option<T>>
2020
where
2121
T: FromPyObject<'py>,
@@ -32,7 +32,7 @@ where
3232
pub fn schema_or_config_same<'py, T>(
3333
schema: &'py PyDict,
3434
config: Option<&'py PyDict>,
35-
key: &PyString,
35+
key: &Bound<'py, PyString>,
3636
) -> PyResult<Option<T>>
3737
where
3838
T: FromPyObject<'py>,

src/errors/location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use pyo3::exceptions::PyTypeError;
2-
use pyo3::once_cell::GILOnceCell;
2+
use pyo3::sync::GILOnceCell;
33
use std::fmt;
44

55
use pyo3::prelude::*;

src/errors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::value_exception::{PydanticCustomError, PydanticKnownError, Pydanti
1414

1515
pub fn py_err_string(py: Python, err: PyErr) -> String {
1616
let value = err.value(py);
17-
match value.get_type().name() {
17+
match value.get_type().qualname() {
1818
Ok(type_name) => match value.str() {
1919
Ok(py_str) => {
2020
let str_cow = py_str.to_string_lossy();

src/errors/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::borrow::Cow;
33
use std::fmt;
44

55
use pyo3::exceptions::{PyKeyError, PyTypeError};
6-
use pyo3::once_cell::GILOnceCell;
76
use pyo3::prelude::*;
7+
use pyo3::sync::GILOnceCell;
88
use pyo3::types::{PyDict, PyList};
99

1010
use ahash::AHashMap;

src/errors/validation_exception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::str::from_utf8;
55
use pyo3::exceptions::{PyKeyError, PyTypeError, PyValueError};
66
use pyo3::ffi;
77
use pyo3::intern;
8-
use pyo3::once_cell::GILOnceCell;
98
use pyo3::prelude::*;
9+
use pyo3::sync::GILOnceCell;
1010
use pyo3::types::{PyDict, PyList, PyString};
1111
use serde::ser::{Error, SerializeMap, SerializeSeq};
1212
use serde::{Serialize, Serializer};
@@ -559,7 +559,7 @@ impl PyLineError {
559559
let input_str = safe_repr(input_value);
560560
truncate_input_value!(output, &input_str);
561561

562-
if let Ok(type_) = input_value.get_type().name() {
562+
if let Ok(type_) = input_value.get_type().qualname() {
563563
write!(output, ", input_type={type_}")?;
564564
}
565565
}

src/input/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a> TryFrom<&'a PyAny> for EitherTimedelta<'a> {
114114
type Error = PyErr;
115115

116116
fn try_from(value: &'a PyAny) -> PyResult<Self> {
117-
if let Ok(dt) = <PyDelta as PyTryFrom>::try_from_exact(value) {
117+
if let Ok(dt) = value.downcast_exact::<PyDelta>() {
118118
Ok(EitherTimedelta::PyExact(dt))
119119
} else {
120120
let dt = value.downcast::<PyDelta>()?;

src/input/input_abstract.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ pub enum InputType {
2222
impl IntoPy<PyObject> for InputType {
2323
fn into_py(self, py: Python<'_>) -> PyObject {
2424
match self {
25-
Self::Json => intern!(py, "json").into(),
26-
Self::Python => intern!(py, "python").into(),
27-
Self::String => intern!(py, "string").into(),
25+
Self::Json => intern!(py, "json").into_py(py),
26+
Self::Python => intern!(py, "python").into_py(py),
27+
Self::String => intern!(py, "string").into_py(py),
2828
}
2929
}
3030
}

src/input/input_python.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl<'a> Input<'a> for PyAny {
340340
}
341341

342342
fn exact_str(&'a self) -> ValResult<EitherString<'a>> {
343-
if let Ok(py_str) = <PyString as PyTryFrom>::try_from_exact(self) {
343+
if let Ok(py_str) = self.downcast_exact() {
344344
Ok(EitherString::Py(py_str))
345345
} else {
346346
Err(ValError::new(ErrorTypeDefaults::IntType, self))
@@ -389,7 +389,7 @@ impl<'a> Input<'a> for PyAny {
389389

390390
Err(ValError::new(
391391
ErrorType::IsInstanceOf {
392-
class: decimal_type.name().unwrap_or("Decimal").to_string(),
392+
class: decimal_type.qualname().unwrap_or_else(|_| "Decimal".to_owned()),
393393
context: None,
394394
},
395395
self,
@@ -773,7 +773,7 @@ fn maybe_as_enum(v: &PyAny) -> Option<&PyAny> {
773773
}
774774

775775
#[cfg(PyPy)]
776-
static DICT_KEYS_TYPE: pyo3::once_cell::GILOnceCell<Py<PyType>> = pyo3::once_cell::GILOnceCell::new();
776+
static DICT_KEYS_TYPE: pyo3::sync::GILOnceCell<Py<PyType>> = pyo3::sync::GILOnceCell::new();
777777

778778
#[cfg(PyPy)]
779779
fn is_dict_keys_type(v: &PyAny) -> bool {
@@ -791,7 +791,7 @@ fn is_dict_keys_type(v: &PyAny) -> bool {
791791
}
792792

793793
#[cfg(PyPy)]
794-
static DICT_VALUES_TYPE: pyo3::once_cell::GILOnceCell<Py<PyType>> = pyo3::once_cell::GILOnceCell::new();
794+
static DICT_VALUES_TYPE: pyo3::sync::GILOnceCell<Py<PyType>> = pyo3::sync::GILOnceCell::new();
795795

796796
#[cfg(PyPy)]
797797
fn is_dict_values_type(v: &PyAny) -> bool {
@@ -809,7 +809,7 @@ fn is_dict_values_type(v: &PyAny) -> bool {
809809
}
810810

811811
#[cfg(PyPy)]
812-
static DICT_ITEMS_TYPE: pyo3::once_cell::GILOnceCell<Py<PyType>> = pyo3::once_cell::GILOnceCell::new();
812+
static DICT_ITEMS_TYPE: pyo3::sync::GILOnceCell<Py<PyType>> = pyo3::sync::GILOnceCell::new();
813813

814814
#[cfg(PyPy)]
815815
fn is_dict_items_type(v: &PyAny) -> bool {

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ pub use validators::{validate_core_schema, PySome, SchemaValidator};
3939
use crate::input::Input;
4040

4141
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=true))]
42-
pub fn from_json(py: Python, data: &PyAny, allow_inf_nan: bool, cache_strings: bool) -> PyResult<PyObject> {
42+
pub fn from_json<'py>(
43+
py: Python<'py>,
44+
data: &PyAny,
45+
allow_inf_nan: bool,
46+
cache_strings: bool,
47+
) -> PyResult<Bound<'py, PyAny>> {
4348
let v_match = data
4449
.validate_bytes(false)
4550
.map_err(|_| PyTypeError::new_err("Expected bytes, bytearray or str"))?;

src/serializers/extra.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,10 @@ impl CollectWarnings {
333333

334334
fn fallback_warning(&self, field_type: &str, value: &PyAny) {
335335
if self.active {
336-
let type_name = value.get_type().name().unwrap_or("<unknown python object>");
336+
let type_name = value
337+
.get_type()
338+
.qualname()
339+
.unwrap_or_else(|_| "<unknown python object>".to_owned());
337340
self.add_warning(format!(
338341
"Expected `{field_type}` but got `{type_name}` - serialized value may not be as expected"
339342
));

src/serializers/filter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn map_negative_index(value: &PyAny, len: Option<usize>) -> PyResult<&PyAny> {
2323
.map_or_else(|_| value, |v| v)),
2424
None => {
2525
// check that it's not negative
26-
let negative = value.call_method1(intern!(py, "__lt__"), (0,))?.is_true()?;
26+
let negative = value.call_method1(intern!(py, "__lt__"), (0,))?.is_truthy()?;
2727
if negative {
2828
Err(PyValueError::new_err(
2929
"Negative indices cannot be used to exclude items on unsized iterables",
@@ -291,7 +291,7 @@ fn check_contains(obj: &PyAny, py_key: impl ToPyObject + Copy) -> PyResult<Optio
291291
Ok(contains_method) => {
292292
if let Ok(result) = contains_method.call1((py_key.to_object(py),)) {
293293
Ok(Some(
294-
result.is_true()? || contains_method.call1((intern!(py, "__all__"),))?.is_true()?,
294+
result.is_truthy()? || contains_method.call1((intern!(py, "__all__"),))?.is_truthy()?,
295295
))
296296
} else {
297297
Ok(None)
@@ -316,7 +316,7 @@ where
316316

317317
/// detect both ellipsis and `True` to be compatible with pydantic V1
318318
fn is_ellipsis_like(v: &PyAny) -> bool {
319-
v.is_ellipsis()
319+
v.is(&v.py().Ellipsis())
320320
|| match v.downcast::<PyBool>() {
321321
Ok(b) => b.is_true(),
322322
Err(_) => false,

src/serializers/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ fn unknown_type_error(value: &PyAny) -> PyErr {
548548
fn serialize_unknown(value: &PyAny) -> Cow<str> {
549549
if let Ok(s) = value.str() {
550550
s.to_string_lossy()
551-
} else if let Ok(name) = value.get_type().name() {
551+
} else if let Ok(name) = value.get_type().qualname() {
552552
format!("<Unserializable {name} object>").into()
553553
} else {
554554
"<Unserializable object>".into()

src/serializers/ob_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use pyo3::once_cell::GILOnceCell;
21
use pyo3::prelude::*;
2+
use pyo3::sync::GILOnceCell;
33
use pyo3::types::{
44
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDelta, PyDict, PyFloat, PyFrozenSet, PyInt, PyIterator, PyList,
55
PySet, PyString, PyTime, PyTuple, PyType,

src/serializers/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::borrow::Cow;
22
use std::fmt::Debug;
33

44
use pyo3::exceptions::PyTypeError;
5-
use pyo3::once_cell::GILOnceCell;
65
use pyo3::prelude::*;
6+
use pyo3::sync::GILOnceCell;
77
use pyo3::types::{PyDict, PyString};
88
use pyo3::{intern, PyTraverseError, PyVisit};
99

src/serializers/type_serializers/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ impl FormatSerializer {
9595
format!(
9696
"Error calling `format(value, {})`: {}",
9797
self.formatting_string
98-
.as_ref(py)
98+
.bind(py)
9999
.repr()
100-
.unwrap_or_else(|_| intern!(py, "???")),
100+
.unwrap_or_else(|_| intern!(py, "???").clone()),
101101
e
102102
)
103103
})

src/serializers/type_serializers/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub(crate) fn to_str_json_key(key: &PyAny) -> PyResult<Cow<str>> {
179179
build_simple_serializer!(IntSerializer, "int", Int, ObType::Int, to_str_json_key, true);
180180

181181
pub(crate) fn bool_json_key(key: &PyAny) -> PyResult<Cow<str>> {
182-
let v = if key.is_true().unwrap_or(false) {
182+
let v = if key.is_truthy().unwrap_or(false) {
183183
"true"
184184
} else {
185185
"false"

0 commit comments

Comments
 (0)