Trait pyo3::conversion::FromPyObject
source · pub trait FromPyObject<'source>: Sized {
// Required method
fn extract(ob: &'source PyAny) -> PyResult<Self>;
// Provided method
fn type_input() -> TypeInfo { ... }
}
Expand description
Extract a type from a Python object.
Normal usage is through the extract
methods on Py
and PyAny
, which forward to this trait.
Examples
use pyo3::prelude::*;
use pyo3::types::PyString;
Python::with_gil(|py| {
let obj: Py<PyString> = PyString::new(py, "blah").into();
// Straight from an owned reference
let s: &str = obj.extract(py)?;
// Or from a borrowed reference
let obj: &PyString = obj.as_ref(py);
let s: &str = obj.extract()?;
})
Note: depending on the implementation, the lifetime of the extracted result may
depend on the lifetime of the obj
or the prepared
variable.
For example, when extracting &str
from a Python byte string, the resulting string slice will
point to the existing string data (lifetime: 'source
).
On the other hand, when extracting &str
from a Python Unicode string, the preparation step
will convert the string to UTF-8, and the resulting string slice will have lifetime 'prepared
.
Since which case applies depends on the runtime type of the Python object,
both the obj
and prepared
variables must outlive the resulting string slice.
The trait’s conversion method takes a &PyAny
argument but is called
FromPyObject
for historical reasons.
Required Methods§
Provided Methods§
sourcefn type_input() -> TypeInfo
fn type_input() -> TypeInfo
Extracts the type hint information for this type when it appears as an argument.
For example, Vec<u32>
would return Sequence[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 IntoPy::type_output
.
It may be different for some types, such as Dict
, to allow duck-typing: functions return Dict
but take Mapping
as argument.
Implementations on Foreign Types§
source§impl<'source> FromPyObject<'source> for NonZeroI32
impl<'source> FromPyObject<'source> for NonZeroI32
source§impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>where
K: FromPyObject<'source> + Eq + Hash,
S: BuildHasher + Default,
impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>where K: FromPyObject<'source> + Eq + Hash, S: BuildHasher + Default,
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
source§impl FromPyObject<'_> for FixedOffset
impl FromPyObject<'_> for FixedOffset
source§fn extract(ob: &PyAny) -> PyResult<FixedOffset>
fn extract(ob: &PyAny) -> PyResult<FixedOffset>
Convert python tzinfo to rust FixedOffset
.
Note that the conversion will result in precision lost in microseconds as chrono offset does not supports microseconds.
source§impl<'source> FromPyObject<'source> for i64
impl<'source> FromPyObject<'source> for i64
source§impl<'source> FromPyObject<'source> for &'source str
impl<'source> FromPyObject<'source> for &'source str
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
source§impl<'source> FromPyObject<'source> for u16
impl<'source> FromPyObject<'source> for u16
source§impl<'source> FromPyObject<'source> for BigUint
impl<'source> FromPyObject<'source> for BigUint
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
source§impl<'source> FromPyObject<'source> for NonZeroI8
impl<'source> FromPyObject<'source> for NonZeroI8
source§impl<'a> FromPyObject<'a> for &'a [u8]
impl<'a> FromPyObject<'a> for &'a [u8]
source§impl<'source, K, V, S> FromPyObject<'source> for HashMap<K, V, S>where
K: FromPyObject<'source> + Eq + Hash,
V: FromPyObject<'source>,
S: BuildHasher + Default,
impl<'source, K, V, S> FromPyObject<'source> for HashMap<K, V, S>where K: FromPyObject<'source> + Eq + Hash, V: FromPyObject<'source>, S: BuildHasher + Default,
source§impl<'source> FromPyObject<'source> for u8
impl<'source> FromPyObject<'source> for u8
source§impl<'source> FromPyObject<'source> for u128
impl<'source> FromPyObject<'source> for u128
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>> FromPyObject<'s> for (T0, T1)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>> FromPyObject<'s> for (T0, T1)
source§impl<'source> FromPyObject<'source> for usize
impl<'source> FromPyObject<'source> for usize
source§impl FromPyObject<'_> for DateTime<Utc>
impl FromPyObject<'_> for DateTime<Utc>
source§impl<'source> FromPyObject<'source> for u64
impl<'source> FromPyObject<'source> for u64
source§impl<'a, T: FromPyObject<'a>> FromPyObject<'a> for Cell<T>
impl<'a, T: FromPyObject<'a>> FromPyObject<'a> for Cell<T>
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5)
source§impl<'source, K, V, S> FromPyObject<'source> for IndexMap<K, V, S>where
K: FromPyObject<'source> + Eq + Hash,
V: FromPyObject<'source>,
S: BuildHasher + Default,
impl<'source, K, V, S> FromPyObject<'source> for IndexMap<K, V, S>where K: FromPyObject<'source> + Eq + Hash, V: FromPyObject<'source>, S: BuildHasher + Default,
source§impl FromPyObject<'_> for char
impl FromPyObject<'_> for char
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6)
source§impl<'source> FromPyObject<'source> for BigInt
impl<'source> FromPyObject<'source> for BigInt
source§impl<'source> FromPyObject<'source> for bool
impl<'source> FromPyObject<'source> for bool
Converts a Python bool
to a Rust bool
.
Fails with TypeError
if the input is not a Python bool
.
source§impl<'source> FromPyObject<'source> for NonZeroIsize
impl<'source> FromPyObject<'source> for NonZeroIsize
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7)
source§impl<'a, T, const N: usize> FromPyObject<'a> for [T; N]where
T: FromPyObject<'a>,
impl<'a, T, const N: usize> FromPyObject<'a> for [T; N]where T: FromPyObject<'a>,
source§impl<'a, T> FromPyObject<'a> for Vec<T>where
T: FromPyObject<'a>,
impl<'a, T> FromPyObject<'a> for Vec<T>where T: FromPyObject<'a>,
source§impl<'source> FromPyObject<'source> for NonZeroU32
impl<'source> FromPyObject<'source> for NonZeroU32
source§impl<'source> FromPyObject<'source> for NonZeroI64
impl<'source> FromPyObject<'source> for NonZeroI64
source§impl<'source> FromPyObject<'source> for Complex<f32>
impl<'source> FromPyObject<'source> for Complex<f32>
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4)
source§impl<'a, T> FromPyObject<'a> for Option<T>where
T: FromPyObject<'a>,
impl<'a, T> FromPyObject<'a> for Option<T>where T: FromPyObject<'a>,
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2)
source§impl<'source> FromPyObject<'source> for NonZeroI16
impl<'source> FromPyObject<'source> for NonZeroI16
source§impl<'source, K> FromPyObject<'source> for BTreeSet<K>where
K: FromPyObject<'source> + Ord,
impl<'source, K> FromPyObject<'source> for BTreeSet<K>where K: FromPyObject<'source> + Ord,
source§impl<'source> FromPyObject<'source> for i128
impl<'source> FromPyObject<'source> for i128
source§impl<'a, A> FromPyObject<'a> for SmallVec<A>where
A: Array,
A::Item: FromPyObject<'a>,
impl<'a, A> FromPyObject<'a> for SmallVec<A>where A: Array, A::Item: FromPyObject<'a>,
source§impl<'source> FromPyObject<'source> for u32
impl<'source> FromPyObject<'source> for u32
source§impl<'source> FromPyObject<'source> for NonZeroI128
impl<'source> FromPyObject<'source> for NonZeroI128
source§impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>where
K: FromPyObject<'source> + Eq + Hash,
S: BuildHasher + Default,
impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>where K: FromPyObject<'source> + Eq + Hash, S: BuildHasher + Default,
source§impl<'source> FromPyObject<'source> for NonZeroU8
impl<'source> FromPyObject<'source> for NonZeroU8
source§impl<'source> FromPyObject<'source> for isize
impl<'source> FromPyObject<'source> for isize
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>, T11: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>, T11: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
source§impl<'source> FromPyObject<'source> for NonZeroU16
impl<'source> FromPyObject<'source> for NonZeroU16
source§impl<'source> FromPyObject<'source> for Cow<'source, [u8]>
impl<'source> FromPyObject<'source> for Cow<'source, [u8]>
Special-purpose trait impl to efficiently handle both bytes
and bytearray
If the source object is a bytes
object, the Cow
will be borrowed and
pointing into the source object, and no copying or heap allocations will happen.
If it is a bytearray
, its contents will be copied to an owned Cow
.
source§impl<'source> FromPyObject<'source> for Complex<f64>
impl<'source> FromPyObject<'source> for Complex<f64>
source§impl<'source> FromPyObject<'source> for NonZeroU64
impl<'source> FromPyObject<'source> for NonZeroU64
source§impl<'source, K, V, S> FromPyObject<'source> for HashMap<K, V, S>where
K: FromPyObject<'source> + Eq + Hash,
V: FromPyObject<'source>,
S: BuildHasher + Default,
impl<'source, K, V, S> FromPyObject<'source> for HashMap<K, V, S>where K: FromPyObject<'source> + Eq + Hash, V: FromPyObject<'source>, S: BuildHasher + Default,
source§impl<'source> FromPyObject<'source> for NonZeroUsize
impl<'source> FromPyObject<'source> for NonZeroUsize
source§impl<'source, K, V> FromPyObject<'source> for BTreeMap<K, V>where
K: FromPyObject<'source> + Ord,
V: FromPyObject<'source>,
impl<'source, K, V> FromPyObject<'source> for BTreeMap<K, V>where K: FromPyObject<'source> + Ord, V: FromPyObject<'source>,
source§impl<'source> FromPyObject<'source> for f64
impl<'source> FromPyObject<'source> for f64
source§impl<'source> FromPyObject<'source> for f32
impl<'source> FromPyObject<'source> for f32
source§impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
impl<'s, T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
source§impl<'source> FromPyObject<'source> for i32
impl<'source> FromPyObject<'source> for i32
source§impl<'source> FromPyObject<'source> for i16
impl<'source> FromPyObject<'source> for i16
source§impl<'s, T0: FromPyObject<'s>> FromPyObject<'s> for (T0,)
impl<'s, T0: FromPyObject<'s>> FromPyObject<'s> for (T0,)
source§impl FromPyObject<'_> for String
impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.