Skip to content

Commit 856c573

Browse files
authored
Merge pull request #3600 from davidhewitt/pytypecheck
replace `PyTryFrom` by splitting `PyTypeInfo`
2 parents de6d8b7 + ed87637 commit 856c573

20 files changed

+280
-277
lines changed

guide/src/class.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,10 @@ struct MyClass {
11081108
# #[allow(dead_code)]
11091109
num: i32,
11101110
}
1111-
unsafe impl pyo3::type_object::PyTypeInfo for MyClass {
1111+
unsafe impl pyo3::type_object::HasPyGilRef for MyClass {
11121112
type AsRefTarget = pyo3::PyCell<Self>;
1113+
}
1114+
unsafe impl pyo3::type_object::PyTypeInfo for MyClass {
11131115
const NAME: &'static str = "MyClass";
11141116
const MODULE: ::std::option::Option<&'static str> = ::std::option::Option::None;
11151117
#[inline]

guide/src/migration.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@ For a detailed list of all changes, see the [CHANGELOG](changelog.md).
55

66
## from 0.20.* to 0.21
77

8+
### `PyTypeInfo` and `PyTryFrom` have been adjusted
9+
10+
The `PyTryFrom` trait has aged poorly, its [`try_from`] method now conflicts with `try_from` in the 2021 edition prelude. A lot of its functionality was also duplicated with `PyTypeInfo`.
11+
12+
To tighten up the PyO3 traits ahead of [a proposed upcoming API change](https://github.com/PyO3/pyo3/issues/3382) the `PyTypeInfo` trait has had a simpler companion `PyTypeCheck`. The methods [`PyAny::downcast`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyAny.html#method.downcast) and [`PyAny::downcast_exact`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyAny.html#method.downcast_exact) no longer use `PyTryFrom` as a bound, instead using `PyTypeCheck` and `PyTypeInfo` respectively.
13+
14+
To migrate, switch all type casts to use `obj.downcast()` instead of `try_from(obj)` (and similar for `downcast_exact`).
15+
16+
Before:
17+
18+
```rust
19+
# use pyo3::prelude::*;
20+
# use pyo3::types::{PyInt, PyList};
21+
# fn main() -> PyResult<()> {
22+
Python::with_gil(|py| {
23+
let list = PyList::new(py, 0..5);
24+
let b = <PyInt as PyTryFrom>::try_from(list.get_item(0).unwrap())?;
25+
Ok(())
26+
})
27+
# }
28+
```
29+
30+
After:
31+
32+
```rust
33+
# use pyo3::prelude::*;
34+
# use pyo3::types::{PyInt, PyList};
35+
# fn main() -> PyResult<()> {
36+
Python::with_gil(|py| {
37+
let list = PyList::new(py, 0..5);
38+
let b = list.get_item(0).unwrap().downcast::<PyInt>()?;
39+
Ok(())
40+
})
41+
# }
42+
```
43+
844
### `py.None()`, `py.NotImplemented()` and `py.Ellipsis()` now return typed singletons
945

1046
Previously `py.None()`, `py.NotImplemented()` and `py.Ellipsis()` would return `PyObject`. This had a few downsides:

newsfragments/3600.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Split some `PyTypeInfo` functionality into new traits `HasPyGilRef` and `PyTypeCheck`.

pyo3-macros-backend/src/pyclass.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,11 @@ fn impl_pytypeinfo(
741741
};
742742

743743
quote! {
744-
unsafe impl _pyo3::type_object::PyTypeInfo for #cls {
744+
unsafe impl _pyo3::type_object::HasPyGilRef for #cls {
745745
type AsRefTarget = _pyo3::PyCell<Self>;
746+
}
746747

748+
unsafe impl _pyo3::type_object::PyTypeInfo for #cls {
747749
const NAME: &'static str = #cls_name;
748750
const MODULE: ::std::option::Option<&'static str> = #module;
749751

src/conversion.rs

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ where
304304
T: PyClass,
305305
{
306306
fn extract(obj: &'a PyAny) -> PyResult<Self> {
307-
PyTryFrom::try_from(obj).map_err(Into::into)
307+
obj.downcast().map_err(Into::into)
308308
}
309309
}
310310

@@ -313,7 +313,7 @@ where
313313
T: PyClass + Clone,
314314
{
315315
fn extract(obj: &'a PyAny) -> PyResult<Self> {
316-
let cell: &PyCell<Self> = PyTryFrom::try_from(obj)?;
316+
let cell: &PyCell<Self> = obj.downcast()?;
317317
Ok(unsafe { cell.try_borrow_unguarded()?.clone() })
318318
}
319319
}
@@ -323,7 +323,7 @@ where
323323
T: PyClass,
324324
{
325325
fn extract(obj: &'a PyAny) -> PyResult<Self> {
326-
let cell: &PyCell<T> = PyTryFrom::try_from(obj)?;
326+
let cell: &PyCell<T> = obj.downcast()?;
327327
cell.try_borrow().map_err(Into::into)
328328
}
329329
}
@@ -333,7 +333,7 @@ where
333333
T: PyClass<Frozen = False>,
334334
{
335335
fn extract(obj: &'a PyAny) -> PyResult<Self> {
336-
let cell: &PyCell<T> = PyTryFrom::try_from(obj)?;
336+
let cell: &PyCell<T> = obj.downcast()?;
337337
cell.try_borrow_mut().map_err(Into::into)
338338
}
339339
}
@@ -381,78 +381,61 @@ pub trait PyTryInto<T>: Sized {
381381
fn try_into_exact(&self) -> Result<&T, PyDowncastError<'_>>;
382382
}
383383

384-
// TryFrom implies TryInto
385-
impl<U> PyTryInto<U> for PyAny
386-
where
387-
U: for<'v> PyTryFrom<'v>,
388-
{
389-
fn try_into(&self) -> Result<&U, PyDowncastError<'_>> {
390-
<U as PyTryFrom<'_>>::try_from(self)
391-
}
392-
fn try_into_exact(&self) -> Result<&U, PyDowncastError<'_>> {
393-
U::try_from_exact(self)
394-
}
395-
}
384+
mod implementations {
385+
use super::*;
396386

397-
impl<'v, T> PyTryFrom<'v> for T
398-
where
399-
T: PyTypeInfo + PyNativeType,
400-
{
401-
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
402-
let value = value.into();
403-
unsafe {
404-
if T::is_type_of(value) {
405-
Ok(Self::try_from_unchecked(value))
406-
} else {
407-
Err(PyDowncastError::new(value, T::NAME))
408-
}
387+
// TryFrom implies TryInto
388+
impl<U> PyTryInto<U> for PyAny
389+
where
390+
U: for<'v> PyTryFrom<'v>,
391+
{
392+
fn try_into(&self) -> Result<&U, PyDowncastError<'_>> {
393+
<U as PyTryFrom<'_>>::try_from(self)
394+
}
395+
fn try_into_exact(&self) -> Result<&U, PyDowncastError<'_>> {
396+
U::try_from_exact(self)
409397
}
410398
}
411399

412-
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
413-
let value = value.into();
414-
unsafe {
415-
if T::is_exact_type_of(value) {
416-
Ok(Self::try_from_unchecked(value))
417-
} else {
418-
Err(PyDowncastError::new(value, T::NAME))
419-
}
400+
impl<'v, T> PyTryFrom<'v> for T
401+
where
402+
T: PyTypeInfo<AsRefTarget = Self> + PyNativeType,
403+
{
404+
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
405+
value.into().downcast()
420406
}
421-
}
422407

423-
#[inline]
424-
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
425-
Self::unchecked_downcast(value.into())
426-
}
427-
}
408+
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
409+
value.into().downcast_exact()
410+
}
428411

429-
impl<'v, T> PyTryFrom<'v> for PyCell<T>
430-
where
431-
T: 'v + PyClass,
432-
{
433-
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
434-
let value = value.into();
435-
unsafe {
436-
if T::is_type_of(value) {
437-
Ok(Self::try_from_unchecked(value))
438-
} else {
439-
Err(PyDowncastError::new(value, T::NAME))
440-
}
412+
#[inline]
413+
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
414+
value.into().downcast_unchecked()
441415
}
442416
}
443-
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
444-
let value = value.into();
445-
unsafe {
446-
if T::is_exact_type_of(value) {
447-
Ok(Self::try_from_unchecked(value))
448-
} else {
449-
Err(PyDowncastError::new(value, T::NAME))
417+
418+
impl<'v, T> PyTryFrom<'v> for PyCell<T>
419+
where
420+
T: 'v + PyClass,
421+
{
422+
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
423+
value.into().downcast()
424+
}
425+
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
426+
let value = value.into();
427+
unsafe {
428+
if T::is_exact_type_of(value) {
429+
Ok(Self::try_from_unchecked(value))
430+
} else {
431+
Err(PyDowncastError::new(value, T::NAME))
432+
}
450433
}
451434
}
452-
}
453-
#[inline]
454-
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
455-
Self::unchecked_downcast(value.into())
435+
#[inline]
436+
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
437+
value.into().downcast_unchecked()
438+
}
456439
}
457440
}
458441

@@ -572,10 +555,11 @@ mod test_no_clone {}
572555

573556
#[cfg(test)]
574557
mod tests {
575-
use crate::types::{IntoPyDict, PyAny, PyDict, PyList};
576-
use crate::{PyObject, Python, ToPyObject};
558+
use crate::PyObject;
577559

578-
use super::PyTryFrom;
560+
use super::super::PyTryFrom;
561+
use crate::types::{IntoPyDict, PyAny, PyDict, PyList};
562+
use crate::{Python, ToPyObject};
579563

580564
#[test]
581565
fn test_try_from() {

src/instance.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::conversion::PyTryFrom;
21
use crate::err::{self, PyDowncastError, PyErr, PyResult};
3-
use crate::gil;
42
use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell};
53
use crate::pyclass::boolean_struct::{False, True};
4+
use crate::type_object::HasPyGilRef;
65
use crate::types::any::PyAnyMethods;
76
use crate::types::{PyDict, PyString, PyTuple};
87
use crate::{
98
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyClass, PyClassInitializer, PyRef, PyRefMut,
109
PyTypeInfo, Python, ToPyObject,
1110
};
11+
use crate::{gil, PyTypeCheck};
1212
use std::marker::PhantomData;
1313
use std::mem::{self, ManuallyDrop};
1414
use std::ops::Deref;
@@ -185,7 +185,7 @@ impl<'py, T> Py2<'py, T> {
185185
#[doc(hidden)] // public and doc(hidden) to use in examples and tests for now
186186
pub fn as_gil_ref(&'py self) -> &'py T::AsRefTarget
187187
where
188-
T: PyTypeInfo,
188+
T: HasPyGilRef,
189189
{
190190
unsafe { self.py().from_borrowed_ptr(self.as_ptr()) }
191191
}
@@ -194,7 +194,7 @@ impl<'py, T> Py2<'py, T> {
194194
#[doc(hidden)] // public but hidden, to use for tests for now
195195
pub fn into_gil_ref(self) -> &'py T::AsRefTarget
196196
where
197-
T: PyTypeInfo,
197+
T: HasPyGilRef,
198198
{
199199
unsafe { self.py().from_owned_ptr(self.into_ptr()) }
200200
}
@@ -437,7 +437,7 @@ where
437437

438438
impl<T> Py<T>
439439
where
440-
T: PyTypeInfo,
440+
T: HasPyGilRef,
441441
{
442442
/// Borrows a GIL-bound reference to the contained `T`.
443443
///
@@ -1314,11 +1314,11 @@ impl PyObject {
13141314
/// # }
13151315
/// ```
13161316
#[inline]
1317-
pub fn downcast<'p, T>(&'p self, py: Python<'p>) -> Result<&T, PyDowncastError<'_>>
1317+
pub fn downcast<'py, T>(&'py self, py: Python<'py>) -> Result<&'py T, PyDowncastError<'py>>
13181318
where
1319-
T: PyTryFrom<'p>,
1319+
T: PyTypeCheck<AsRefTarget = T>,
13201320
{
1321-
<T as PyTryFrom<'_>>::try_from(self.as_ref(py))
1321+
self.as_ref(py).downcast()
13221322
}
13231323

13241324
/// Casts the PyObject to a concrete Python object type without checking validity.
@@ -1329,9 +1329,9 @@ impl PyObject {
13291329
#[inline]
13301330
pub unsafe fn downcast_unchecked<'p, T>(&'p self, py: Python<'p>) -> &T
13311331
where
1332-
T: PyTryFrom<'p>,
1332+
T: HasPyGilRef<AsRefTarget = T>,
13331333
{
1334-
<T as PyTryFrom<'_>>::try_from_unchecked(self.as_ref(py))
1334+
self.as_ref(py).downcast_unchecked()
13351335
}
13361336
}
13371337

@@ -1509,6 +1509,8 @@ a = A()
15091509

15101510
#[cfg(feature = "macros")]
15111511
mod using_macros {
1512+
use crate::{PyCell, PyTryInto};
1513+
15121514
use super::*;
15131515

15141516
#[crate::pyclass]
@@ -1533,5 +1535,15 @@ a = A()
15331535
assert_eq!(instance.try_borrow_mut(py).unwrap().0, 123);
15341536
})
15351537
}
1538+
1539+
#[test]
1540+
fn cell_tryfrom() {
1541+
// More detailed tests of the underlying semantics in pycell.rs
1542+
Python::with_gil(|py| {
1543+
let instance: &PyAny = Py::new(py, SomeClass(0)).unwrap().into_ref(py);
1544+
let _: &PyCell<SomeClass> = PyTryInto::try_into(instance).unwrap();
1545+
let _: &PyCell<SomeClass> = PyTryInto::try_into_exact(instance).unwrap();
1546+
})
1547+
}
15361548
}
15371549
}

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,8 @@
294294
//! [Features chapter of the guide]: https://pyo3.rs/latest/features.html#features-reference "Features Reference - PyO3 user guide"
295295
//! [`Ungil`]: crate::marker::Ungil
296296
pub use crate::class::*;
297-
pub use crate::conversion::{
298-
AsPyPointer, FromPyObject, FromPyPointer, IntoPy, PyTryFrom, PyTryInto, ToPyObject,
299-
};
297+
pub use crate::conversion::{AsPyPointer, FromPyObject, FromPyPointer, IntoPy, ToPyObject};
298+
pub use crate::conversion::{PyTryFrom, PyTryInto};
300299
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyResult};
301300
pub use crate::gil::GILPool;
302301
#[cfg(not(PyPy))]
@@ -306,7 +305,7 @@ pub use crate::marker::Python;
306305
pub use crate::pycell::{PyCell, PyRef, PyRefMut};
307306
pub use crate::pyclass::PyClass;
308307
pub use crate::pyclass_init::PyClassInitializer;
309-
pub use crate::type_object::PyTypeInfo;
308+
pub use crate::type_object::{PyTypeCheck, PyTypeInfo};
310309
pub use crate::types::PyAny;
311310
pub use crate::version::PythonVersionInfo;
312311

0 commit comments

Comments
 (0)