pub trait FromPyObject<'py>: Sized {
// Required method
fn extract_bound(ob: &Bound<'py, 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 Bound
and Py
, which forward to this trait.
§Examples
use pyo3::prelude::*;
use pyo3::types::PyString;
Python::with_gil(|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§
sourcefn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self>
Extracts Self
from the bound smart pointer obj
.
Implementors are encouraged to implement this method and leave extract
defaulted, as
this will be most compatible with PyO3’s future API.
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.
Object Safety§
Implementations on Foreign Types§
source§impl FromPyObject<'_> for IpAddr
impl FromPyObject<'_> for IpAddr
source§impl FromPyObject<'_> for bool
impl FromPyObject<'_> for bool
Converts a Python bool
to a Rust bool
.
Fails with TypeError
if the input is not a Python bool
.
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>
fn type_input() -> TypeInfo
source§impl FromPyObject<'_> for char
impl FromPyObject<'_> for char
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>
fn type_input() -> TypeInfo
source§impl FromPyObject<'_> for i128
impl FromPyObject<'_> for i128
source§impl FromPyObject<'_> for u8
impl FromPyObject<'_> for u8
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>
fn type_input() -> TypeInfo
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
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>
fn type_input() -> TypeInfo
source§impl FromPyObject<'_> for String
impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self>
fn type_input() -> TypeInfo
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
source§impl FromPyObject<'_> for NaiveDate
impl FromPyObject<'_> for NaiveDate
source§impl FromPyObject<'_> for NaiveDateTime
impl FromPyObject<'_> for NaiveDateTime
fn extract_bound(dt: &Bound<'_, PyAny>) -> PyResult<NaiveDateTime>
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>) -> PyResult<FixedOffset>
fn extract_bound(ob: &Bound<'_, 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.