union_type

Function union_type 

Source
pub fn union_type<'py>(
    py: Python<'py>,
    types: &[Bound<'py, PyAny>],
) -> PyResult<Bound<'py, PyAny>>
Expand description

Creates a Python union type using the | operator (Python 3.10+).

§Arguments

  • py - Python interpreter token
  • types - Slice of Python type objects to combine into a union

§Returns

A Python object representing the union of all input types. For a single type, returns that type unchanged. For multiple types, returns a types.UnionType.

§Errors

Returns an error if:

  • The types slice is empty
  • Any of the __or__ operations fail

§Example

use pyo3::types::{PyInt, PyString};

let union = union_type(py, &[
    py.get_type::<PyInt>().into_any(),
    py.get_type::<PyString>().into_any(),
])?;
// union is equivalent to `int | str` in Python