guide/src/conversions/tables.md
When writing functions callable from Python (such as a #[pyfunction] or in a #[pymethods] block), the trait FromPyObject is required for function arguments, and IntoPyObject is required for function return values.
Consult the tables in the following section to find the Rust types provided by PyO3 which implement these traits.
When accepting a function argument, it is possible to either use Rust library types or PyO3's Python-native types. (See the next section for discussion on when to use each.)
The table below contains the Python type and the corresponding function argument types that will accept them:
| Python | Rust | Rust (Python-native) |
|---|---|---|
object | - | PyAny |
str | String, Cow<str>, &str, char, OsString, PathBuf, Path | PyString |
bytes | Vec<u8>, &[u8], Cow<[u8]> | PyBytes |
bool | bool | PyBool |
int | i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, num_bigint::BigInt1, num_bigint::BigUint1 | PyInt |
float | f32, f64, ordered_float::NotNan2, ordered_float::OrderedFloat2 | PyFloat |
complex | num_complex::Complex3 | PyComplex |
fractions.Fraction | num_rational::Ratio4 | - |
list[T] | Vec<T> | PyList |
dict[K, V] | HashMap<K, V>, BTreeMap<K, V>, hashbrown::HashMap<K, V>5, indexmap::IndexMap<K, V>6 | PyDict |
tuple[T, U] | (T, U), Vec<T> | PyTuple |
set[T] | HashSet<T>, BTreeSet<T>, hashbrown::HashSet<T>5 | PySet |
frozenset[T] | HashSet<T>, BTreeSet<T>, hashbrown::HashSet<T>5 | PyFrozenSet |
bytearray | Vec<u8>, Cow<[u8]> | PyByteArray |
slice | - | PySlice |
type | - | PyType |
module | - | PyModule |
collections.abc.Buffer | - | PyBuffer<T> |
datetime.datetime | SystemTime, chrono::DateTime<Tz>7, chrono::NaiveDateTime7 | PyDateTime |
datetime.date | chrono::NaiveDate7 | PyDate |
datetime.time | chrono::NaiveTime7 | PyTime |
datetime.tzinfo | chrono::FixedOffset7, chrono::Utc7, chrono_tz::TimeZone8 | PyTzInfo |
datetime.timedelta | Duration, chrono::Duration7 | PyDelta |
decimal.Decimal | rust_decimal::Decimal9 | - |
decimal.Decimal | bigdecimal::BigDecimal10 | - |
ipaddress.IPv4Address | std::net::IpAddr, std::net::Ipv4Addr | - |
ipaddress.IPv6Address | std::net::IpAddr, std::net::Ipv6Addr | - |
os.PathLike | PathBuf, Path | PyString |
pathlib.Path | PathBuf, Path | PyString |
typing.Optional[T] | Option<T> | - |
typing.Sequence[T] | Vec<T> | PySequence |
typing.Mapping[K, V] | HashMap<K, V>, BTreeMap<K, V>, hashbrown::HashMap<K, V>5, indexmap::IndexMap<K, V>6 | &PyMapping |
typing.Iterator[Any] | - | PyIterator |
typing.Union[...] | See #[derive(FromPyObject)] | - |
It is also worth remembering the following special types:
| What | Description |
|---|---|
Python<'py> | A token used to prove attachment to the Python interpreter. |
Bound<'py, T> | A Python object with a lifetime which binds it to the attachment to the Python interpreter. This provides access to most of PyO3's APIs. |
Py<T> | A Python object not connected to any lifetime of attachment to the Python interpreter. This can be sent to other threads. |
PyRef<T> | A #[pyclass] borrowed immutably. |
PyRefMut<T> | A #[pyclass] borrowed mutably. |
For more detail on accepting #[pyclass] values as function arguments, see the section of this guide on Python Classes.
Using Rust library types as function arguments will incur a conversion cost compared to using the Python-native types.
Using the Python-native types is almost zero-cost (they just require a type check similar to the Python builtin function isinstance()).
However, once that conversion cost has been paid, the Rust standard library types offer a number of benefits:
Python::detach to detach from the interpreter and let other Python threads make progress while your Rust code is executing.Vec<i32>, which will only accept a Python list containing integers.
The Python-native equivalent, &PyList, would accept a Python list containing Python objects of any type.For most PyO3 usage the conversion cost is worth paying to get these benefits. As always, if you're not sure it's worth it in your case, benchmark it!
When returning values from functions callable from Python, PyO3's smart pointers (Py<T>, Bound<'py, T>, and Borrowed<'a, 'py, T>) can be used with zero cost.
Because Bound<'py, T> and Borrowed<'a, 'py, T> have lifetime parameters, the Rust compiler may ask for lifetime annotations to be added to your function.
See the section of the guide dedicated to this.
If your function is fallible, it should return PyResult<T> or Result<T, E> where E implements From<E> for PyErr.
This will raise a Python exception if the Err variant is returned.
Finally, the following Rust types are also able to convert to Python as return values:
| Rust type | Resulting Python Type |
|---|---|
String | str |
&str | str |
bool | bool |
Any integer type (i32, u32, usize, etc) | int |
f32, f64 | float |
Option<T> | Optional[T] |
(T, U) | Tuple[T, U] |
Vec<T> | List[T] |
Cow<[u8]> | bytes |
HashMap<K, V> | Dict[K, V] |
BTreeMap<K, V> | Dict[K, V] |
HashSet<T> | Set[T] |
BTreeSet<T> | Set[T] |
Py<T> | T |
Bound<T> | T |
PyRef<T: PyClass> | T |
PyRefMut<T: PyClass> | T |
Requires the num-complex optional feature. ↩
Requires the num-rational optional feature. ↩
Requires the chrono (and maybe chrono-local) optional feature(s). ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7
Requires the chrono-tz optional feature. ↩
Requires the rust_decimal optional feature. ↩
Requires the bigdecimal optional feature. ↩