Skip to content

Commit

Permalink
Merge pull request #1140 from davidhewitt/union-doc
Browse files Browse the repository at this point in the history
Add doc for extracting Union via FromPyObject
  • Loading branch information
davidhewitt authored Sep 1, 2020
2 parents c506589 + f0650eb commit ffe543f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions guide/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The table below contains the Python type and the corresponding function argument
| `typing.Optional[T]` | `Option<T>` | - |
| `typing.Sequence[T]` | `Vec<T>` | `&PySequence` |
| `typing.Iterator[Any]` | - | `&PyIterator` |
| `typing.Union[...]` | See [`#[derive(FromPyObject)]`](#deriving-a-hrefhttpsdocsrspyo3latestpyo3conversiontraitfrompyobjecthtmlfrompyobjecta-for-enums) | - |

There are also a few special types related to the GIL and Rust-defined `#[pyclass]`es which may come in useful:

Expand Down Expand Up @@ -149,7 +150,7 @@ struct RustyStruct {
#[pyo3(item)]
my_string: String,
}
```
```

The argument passed to `getattr` and `get_item` can also be configured:

Expand Down Expand Up @@ -185,7 +186,7 @@ struct RustyTuple(String, String);

Tuple structs with a single field are treated as wrapper types which are described in the
following section. To override this behaviour and ensure that the input is in fact a tuple,
specify the struct as
specify the struct as
```
use pyo3::prelude::*;
Expand Down Expand Up @@ -217,6 +218,7 @@ struct RustyTransparentStruct {

The `FromPyObject` derivation for enums generates code that tries to extract the variants in the
order of the fields. As soon as a variant can be extracted succesfully, that variant is returned.
This makes it possible to extract Python types like `Union[str, int]`.

The same customizations and restrictions described for struct derivations apply to enum variants,
i.e. a tuple variant assumes that the input is a Python tuple, and a struct variant defaults to
Expand Down Expand Up @@ -267,7 +269,7 @@ enum RustyEnum {

If the input is neither a string nor an integer, the error message will be:
`"Can't convert <INPUT> to Union[str, int]"`, where `<INPUT>` is replaced by the type name and
`repr()` of the input object.
`repr()` of the input object.

#### `#[derive(FromPyObject)]` Container Attributes
- `pyo3(transparent)`
Expand Down

0 comments on commit ffe543f

Please sign in to comment.