pub trait FromPyObject<'py>: Sized {
// Required method
fn extract_bound(ob: &Bound<'py, PyAny>) -> Result<Self, PyErr>;
}Expand description
Extract a type from a Python object.
Normal usage is through the extract methods on Bound and Py, which forward to this trait.
§Examples
use pyo3::prelude::*;
use pyo3::types::PyString;
Python::attach(|py| {
// Calling `.extract()` on a `Bound` smart pointer
let obj: Bound<'_, PyString> = PyString::new(py, "blah");
let s: String = obj.extract()?;
// Calling `.extract(py)` on a `Py` smart pointer
let obj: Py<PyString> = obj.unbind();
let s: String = obj.extract(py)?;
})During the migration of PyO3 from the “GIL Refs” API to the Bound<T> smart pointer, this trait
has two methods extract and extract_bound which are defaulted to call each other. To avoid
infinite recursion, implementors must implement at least one of these methods. The recommendation
is to implement extract_bound and leave extract as the default implementation.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl FromPyObject<'_> for IpAddr
impl FromPyObject<'_> for IpAddr
Source§impl FromPyObject<'_> for bool
Converts a Python bool to a Rust bool.
impl FromPyObject<'_> for bool
Converts a Python bool to a Rust bool.
Fails with TypeError if the input is not a Python bool.
Source§impl FromPyObject<'_> for char
impl FromPyObject<'_> for char
Source§impl FromPyObject<'_> for i128
impl FromPyObject<'_> for i128
Source§impl FromPyObject<'_> for u8
impl FromPyObject<'_> for u8
Source§impl FromPyObject<'_> for u64
impl FromPyObject<'_> for u64
Source§impl FromPyObject<'_> for u128
impl FromPyObject<'_> for u128
Source§impl FromPyObject<'_> for usize
impl FromPyObject<'_> for usize
Source§impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str and unicode objects.
impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str and unicode objects.
Source§impl FromPyObject<'_> for NonZero<i8>
impl FromPyObject<'_> for NonZero<i8>
Source§impl FromPyObject<'_> for NonZero<i16>
impl FromPyObject<'_> for NonZero<i16>
Source§impl FromPyObject<'_> for NonZero<i32>
impl FromPyObject<'_> for NonZero<i32>
Source§impl FromPyObject<'_> for NonZero<i64>
impl FromPyObject<'_> for NonZero<i64>
Source§impl FromPyObject<'_> for NonZero<i128>
impl FromPyObject<'_> for NonZero<i128>
Source§impl FromPyObject<'_> for NonZero<isize>
impl FromPyObject<'_> for NonZero<isize>
Source§impl FromPyObject<'_> for NonZero<u8>
impl FromPyObject<'_> for NonZero<u8>
Source§impl FromPyObject<'_> for NonZero<u16>
impl FromPyObject<'_> for NonZero<u16>
Source§impl FromPyObject<'_> for NonZero<u32>
impl FromPyObject<'_> for NonZero<u32>
Source§impl FromPyObject<'_> for NonZero<u64>
impl FromPyObject<'_> for NonZero<u64>
Source§impl FromPyObject<'_> for NonZero<u128>
impl FromPyObject<'_> for NonZero<u128>
Source§impl FromPyObject<'_> for NonZero<usize>
impl FromPyObject<'_> for NonZero<usize>
Source§impl FromPyObject<'_> for Duration
impl FromPyObject<'_> for Duration
Source§impl FromPyObject<'_> for OsString
impl FromPyObject<'_> for OsString
Source§impl FromPyObject<'_> for PathBuf
impl FromPyObject<'_> for PathBuf
Source§impl FromPyObject<'_> for SystemTime
impl FromPyObject<'_> for SystemTime
fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<SystemTime, PyErr>
Source§impl FromPyObject<'_> for NaiveDate
impl FromPyObject<'_> for NaiveDate
Source§impl FromPyObject<'_> for NaiveDateTime
impl FromPyObject<'_> for NaiveDateTime
fn extract_bound(dt: &Bound<'_, PyAny>) -> Result<NaiveDateTime, PyErr>
Source§impl FromPyObject<'_> for NaiveTime
impl FromPyObject<'_> for NaiveTime
Source§impl FromPyObject<'_> for FixedOffset
impl FromPyObject<'_> for FixedOffset
Source§fn extract_bound(ob: &Bound<'_, PyAny>) -> Result<FixedOffset, PyErr>
fn extract_bound(ob: &Bound<'_, PyAny>) -> Result<FixedOffset, PyErr>
Convert python tzinfo to rust FixedOffset.
Note that the conversion will result in precision lost in microseconds as chrono offset does not supports microseconds.