Trait pyo3::conversion::IntoPy

source ·
pub trait IntoPy<T>: Sized {
    // Required method
    fn into_py(self, py: Python<'_>) -> T;

    // Provided method
    fn type_output() -> TypeInfo { ... }
}
Expand description

Defines a conversion from a Rust type to a Python object.

It functions similarly to std’s Into trait, but requires a GIL token as an argument. Many functions and traits internal to PyO3 require this trait as a bound, so a lack of this trait can manifest itself in different error messages.

§Examples

§With #[pyclass]

The easiest way to implement IntoPy is by exposing a struct as a native Python object by annotating it with #[pyclass].

use pyo3::prelude::*;

#[pyclass]
struct Number {
    #[pyo3(get, set)]
    value: i32,
}

Python code will see this as an instance of the Number class with a value attribute.

§Conversion to a Python object

However, it may not be desirable to expose the existence of Number to Python code. IntoPy allows us to define a conversion to an appropriate Python object.

use pyo3::prelude::*;

struct Number {
    value: i32,
}

impl IntoPy<PyObject> for Number {
    fn into_py(self, py: Python<'_>) -> PyObject {
        // delegates to i32's IntoPy implementation.
        self.value.into_py(py)
    }
}

Python code will see this as an int object.

§Dynamic conversion into Python objects.

It is also possible to return a different Python object depending on some condition. This is useful for types like enums that can carry different types.

use pyo3::prelude::*;

enum Value {
    Integer(i32),
    String(String),
    None,
}

impl IntoPy<PyObject> for Value {
    fn into_py(self, py: Python<'_>) -> PyObject {
        match self {
            Self::Integer(val) => val.into_py(py),
            Self::String(val) => val.into_py(py),
            Self::None => py.None(),
        }
    }
}

Python code will see this as any of the int, string or None objects.

Required Methods§

source

fn into_py(self, py: Python<'_>) -> T

Performs the conversion.

Provided Methods§

source

fn type_output() -> TypeInfo

Extracts the type hint information for this type when it appears as a return value.

For example, Vec<u32> would return List[int]. The default implementation returns Any, which is correct for any type.

For most types, the return value for this method will be identical to that of FromPyObject::type_input. It may be different for some types, such as Dict, to allow duck-typing: functions return Dict but take Mapping as argument.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl IntoPy<Py<PyAny>> for &OsStr

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Cow<'_, str>

source§

impl IntoPy<Py<PyAny>> for Cow<'_, OsStr>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Cow<'_, [u8]>

source§

fn into_py(self, py: Python<'_>) -> Py<PyAny>

source§

impl IntoPy<Py<PyAny>> for IpAddr

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for bool

source§

impl IntoPy<Py<PyAny>> for char

source§

impl IntoPy<Py<PyAny>> for f32

source§

impl IntoPy<Py<PyAny>> for f64

source§

impl IntoPy<Py<PyAny>> for i8

source§

impl IntoPy<Py<PyAny>> for i16

source§

impl IntoPy<Py<PyAny>> for i32

source§

impl IntoPy<Py<PyAny>> for i64

source§

impl IntoPy<Py<PyAny>> for i128

source§

impl IntoPy<Py<PyAny>> for isize

source§

impl IntoPy<Py<PyAny>> for u8

source§

impl IntoPy<Py<PyAny>> for u16

source§

impl IntoPy<Py<PyAny>> for u32

source§

impl IntoPy<Py<PyAny>> for u64

source§

impl IntoPy<Py<PyAny>> for u128

source§

impl IntoPy<Py<PyAny>> for ()

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for usize

source§

impl IntoPy<Py<PyAny>> for String

source§

impl IntoPy<Py<PyAny>> for Duration

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for OsString

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for PathBuf

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for SystemTime

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NaiveDate

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NaiveDateTime

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NaiveTime

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for FixedOffset

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Utc

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for BigInt

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for BigUint

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Complex<f32>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Complex<f64>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Ratio<i8>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Ratio<i16>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Ratio<i32>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Ratio<i64>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Ratio<isize>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Ratio<BigInt>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroI8

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroI16

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroI32

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroI64

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroI128

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroIsize

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroU8

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroU16

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroU32

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroU64

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroU128

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for NonZeroUsize

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Duration

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyAny>> for Decimal

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl IntoPy<Py<PyTuple>> for ()

Converts () to an empty Python tuple.

source§

fn into_py(self, py: Python<'_>) -> Py<PyTuple>

source§

impl<'a> IntoPy<Py<PyAny>> for &'a str

source§

impl<'a> IntoPy<Py<PyAny>> for &'a String

source§

impl<'a> IntoPy<Py<PyAny>> for &'a OsString

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<'a> IntoPy<Py<PyAny>> for &'a Path

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<'a> IntoPy<Py<PyAny>> for &'a PathBuf

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<'a> IntoPy<Py<PyAny>> for &'a [u8]

source§

impl<'a> IntoPy<Py<PyAny>> for Cow<'a, Path>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<'a> IntoPy<Py<PyString>> for &'a str

source§

impl<A> IntoPy<Py<PyAny>> for SmallVec<A>
where A: Array, A::Item: IntoPy<PyObject>,

source§

impl<K> IntoPy<Py<PyAny>> for BTreeSet<K>
where K: IntoPy<PyObject> + Ord,

source§

impl<K, S> IntoPy<Py<PyAny>> for HashSet<K, S>
where K: IntoPy<PyObject> + Eq + Hash, S: BuildHasher + Default,

source§

impl<K, S> IntoPy<Py<PyAny>> for HashSet<K, S>
where K: IntoPy<PyObject> + Eq + Hash, S: BuildHasher + Default,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<K, V> IntoPy<Py<PyAny>> for BTreeMap<K, V>
where K: Eq + IntoPy<PyObject>, V: IntoPy<PyObject>,

source§

impl<K, V, H> IntoPy<Py<PyAny>> for HashMap<K, V, H>
where K: Hash + Eq + IntoPy<PyObject>, V: IntoPy<PyObject>, H: BuildHasher,

source§

impl<K, V, H> IntoPy<Py<PyAny>> for HashMap<K, V, H>
where K: Hash + Eq + IntoPy<PyObject>, V: IntoPy<PyObject>, H: BuildHasher,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<K, V, H> IntoPy<Py<PyAny>> for IndexMap<K, V, H>
where K: Hash + Eq + IntoPy<PyObject>, V: IntoPy<PyObject>, H: BuildHasher,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<L, R> IntoPy<Py<PyAny>> for Either<L, R>
where L: IntoPy<PyObject>, R: IntoPy<PyObject>,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<T0: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0,)

source§

impl<T0: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0,)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>, T11: IntoPy<PyObject>> IntoPy<Py<PyAny>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

source§

impl<T0: IntoPy<PyObject>, T1: IntoPy<PyObject>, T2: IntoPy<PyObject>, T3: IntoPy<PyObject>, T4: IntoPy<PyObject>, T5: IntoPy<PyObject>, T6: IntoPy<PyObject>, T7: IntoPy<PyObject>, T8: IntoPy<PyObject>, T9: IntoPy<PyObject>, T10: IntoPy<PyObject>, T11: IntoPy<PyObject>> IntoPy<Py<PyTuple>> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

source§

impl<T> IntoPy<Py<PyAny>> for Option<T>
where T: IntoPy<PyObject>,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<T> IntoPy<Py<PyAny>> for &T
where T: AsRef<PyAny>,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<T> IntoPy<Py<PyAny>> for Vec<T>
where T: IntoPy<PyObject>,

source§

impl<T, const N: usize> IntoPy<Py<PyAny>> for [T; N]
where T: IntoPy<PyObject>,

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<T: Copy + IntoPy<PyObject>> IntoPy<Py<PyAny>> for Cell<T>

source§

fn into_py(self, py: Python<'_>) -> PyObject

source§

impl<Tz: TimeZone> IntoPy<Py<PyAny>> for DateTime<Tz>

source§

fn into_py(self, py: Python<'_>) -> PyObject

Implementors§

source§

impl IntoPy<Py<PyAny>> for &PyAny

source§

impl IntoPy<Py<PyAny>> for Coroutine

source§

impl IntoPy<Py<PyAny>> for PyErr

source§

impl IntoPy<Py<PyAny>> for PyBackedBytes

source§

impl IntoPy<Py<PyAny>> for PyBackedStr

source§

impl IntoPy<Py<PyString>> for &Bound<'_, PyString>

source§

impl IntoPy<Py<PyString>> for &Py<PyString>

source§

impl IntoPy<Py<PyString>> for Bound<'_, PyString>

source§

impl IntoPy<Py<PyTuple>> for &Bound<'_, PyTuple>

source§

impl IntoPy<Py<PyTuple>> for Bound<'_, PyTuple>

source§

impl<'a> IntoPy<Py<PyAny>> for &'a PyErr

source§

impl<T> IntoPy<Py<PyAny>> for &Bound<'_, T>

source§

impl<T> IntoPy<Py<PyAny>> for &Py<T>

source§

impl<T> IntoPy<Py<PyAny>> for Borrowed<'_, '_, T>

source§

impl<T> IntoPy<Py<PyAny>> for Bound<'_, T>

source§

impl<T> IntoPy<Py<PyAny>> for Py<T>

source§

impl<T: PyClass> IntoPy<Py<PyAny>> for &PyRef<'_, T>

source§

impl<T: PyClass> IntoPy<Py<PyAny>> for PyRef<'_, T>

source§

impl<T: PyClass<Frozen = False>> IntoPy<Py<PyAny>> for &PyRefMut<'_, T>

source§

impl<T: PyClass<Frozen = False>> IntoPy<Py<PyAny>> for PyRefMut<'_, T>