pub trait FromPyObject<'s>: Sized {
    fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>;
}
Expand description

FromPyObject is implemented by various types that can be extracted from a Python object.

Normal usage is through the PyObject::extract helper method:

let value = obj.extract::<TargetType>(py)?;

Each target type for this conversion supports a different Python objects as input. Calls with an unsupported input object will result in an exception (usually a TypeError).

This trait is also used by the py_fn! and py_class! and py_argparse! macros in order to translate from Python objects to the expected Rust parameter types. For example, the parameter x in def method(self, x: i32) will use impl FromPyObject for i32 to convert the input Python object into a Rust i32. When these macros are used with reference parameters (x: &str), the trait RefFromPyObject is used instead.

Required Methods

Extracts Self from the source PyObject.

Implementations on Foreign Types

If the python value is None, returns Option::None. Otherwise, converts the python value to T and returns Some(T).

Converts a Python bool to a rust bool.

Fails with TypeError if the input is not a Python bool.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

Converts Python float to Rust f64.

Converts Python float to Rust f32.

This conversion loses precision as the 64-bit float from Python gets converted to a 32-bit float. Out-of-range numbers may also overflow to infinity.

Uses the sequence protocol and converts each individual element via impl FromPyObject for T.

Note: when using --features nightly, a specialization of this impl may use the buffer protocol to perform a more efficient bulk copy.

Allows extracting strings from Python objects. Accepts Python str and unicode objects. In Python 2.7, str is expected to be UTF-8 encoded.

Returns a UnicodeDecodeError if the input is not valid unicode (containing unpaired surrogates, or a Python 2.7 byte string that is not valid UTF-8).

Allows extracting strings from Python objects. Accepts Python str and unicode objects. In Python 2.7, str is expected to be UTF-8 encoded.

Returns a UnicodeDecodeError if the input is not valid unicode (containing unpaired surrogates, or a Python 2.7 byte string that is not valid UTF-8).

For Python bytes, returns a reference to the existing immutable string data. If the Python object is a single-dimensional buffer of format c or B (C: char or unsigned char), returns an owned copy of the data in the buffer. Otherwise, uses the sequence protocol and converts each individual element via impl FromPyObject for u8.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

Implementors