Skip to content

Commit 4a4893e

Browse files
authored
Merge pull request #191 from projectsyn/pyo3/replace-deprecated-downcast
Replace deprecated PyO3 `downcast*` calls with equivalent `cast*` calls
2 parents 246936b + 215688b commit 4a4893e

3 files changed

Lines changed: 9 additions & 12 deletions

File tree

src/types/from.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ impl TryFrom<Bound<'_, PyAny>> for Value {
135135
Ok(Self::String(v.to_string()))
136136
}
137137
"list" => {
138-
let v = value.downcast::<PySequence>()?;
138+
let v = value.cast::<PySequence>()?;
139139
let mut items: Vec<Value> = vec![];
140140
for it in v.try_iter()? {
141141
items.push(TryInto::try_into(it?)?);
142142
}
143143
Ok(Self::Sequence(items))
144144
}
145145
"dict" => {
146-
let dict = value.downcast::<PyDict>()?;
146+
let dict = value.cast::<PyDict>()?;
147147
let mut mapping = crate::types::Mapping::new();
148148
for (k, v) in dict {
149149
let kv = TryInto::try_into(k)?;

src/types/mapping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,10 @@ mod mapping_tests {
796796
assert_eq!(format!("{:?}", m.keys()), "['a', 'b', 'c', 'd', 'e', 'f']");
797797
let a = m.get_item(&"a").unwrap().unwrap();
798798
assert!(a.is_instance_of::<pyo3::types::PyInt>());
799-
assert!(a.downcast_exact::<pyo3::types::PyInt>().unwrap().eq(&1));
799+
assert!(a.cast_exact::<pyo3::types::PyInt>().unwrap().eq(&1));
800800
let f = m.get_item(&"f").unwrap().unwrap();
801801
assert!(f.is_instance_of::<PyDict>());
802-
let f = f.downcast_exact::<PyDict>().unwrap();
802+
let f = f.cast_exact::<PyDict>().unwrap();
803803
assert_eq!(f.len(), 1);
804804
assert_eq!(format!("{:?}", f.keys()), "['foo']");
805805
assert_eq!(format!("{:?}", f.values()), "['bar']");

src/types/value/value_as_py_obj_tests.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn test_as_py_obj_bool() {
1414
Python::attach(|py| {
1515
let b = Value::Bool(true).as_py_obj(py).unwrap();
1616
assert!(b.is_instance_of::<pyo3::types::PyBool>());
17-
assert!(b.downcast_exact::<pyo3::types::PyBool>().unwrap().is_true());
17+
assert!(b.cast_exact::<pyo3::types::PyBool>().unwrap().is_true());
1818
});
1919
}
2020

@@ -27,7 +27,7 @@ fn test_as_py_obj_int() {
2727
let pyn = n.as_py_obj(py).unwrap();
2828
let n = n.as_i64().unwrap();
2929
assert!(pyn.is_instance_of::<pyo3::types::PyInt>());
30-
assert!(pyn.downcast_exact::<pyo3::types::PyInt>().unwrap().eq(&n));
30+
assert!(pyn.cast_exact::<pyo3::types::PyInt>().unwrap().eq(&n));
3131
}
3232
});
3333
}
@@ -39,10 +39,7 @@ fn test_as_py_obj_float() {
3939
let n: Value = 3.14.into();
4040
let n = n.as_py_obj(py).unwrap();
4141
assert!(n.is_instance_of::<pyo3::types::PyFloat>());
42-
assert!(n
43-
.downcast_exact::<pyo3::types::PyFloat>()
44-
.unwrap()
45-
.eq(&3.14));
42+
assert!(n.cast_exact::<pyo3::types::PyFloat>().unwrap().eq(&3.14));
4643
});
4744
}
4845

@@ -54,7 +51,7 @@ fn test_as_py_obj_sequence() {
5451
let s = s.as_py_obj(py).unwrap();
5552
assert!(s.is_instance_of::<pyo3::types::PyList>());
5653
assert!(s
57-
.downcast_exact::<pyo3::types::PyList>()
54+
.cast_exact::<pyo3::types::PyList>()
5855
.unwrap()
5956
.eq(&vec![1, 2, 3])
6057
.unwrap());
@@ -70,7 +67,7 @@ fn test_as_py_obj_string() {
7067
.unwrap();
7168
assert!(s.is_instance_of::<pyo3::types::PyString>());
7269
assert_eq!(
73-
s.downcast_exact::<pyo3::types::PyString>()
70+
s.cast_exact::<pyo3::types::PyString>()
7471
.unwrap()
7572
.to_str()
7673
.unwrap(),

0 commit comments

Comments
 (0)