pep 604: use T | U instead of Union[T, U] in messaging

This commit is contained in:
David Hewitt 2021-09-24 22:46:25 +01:00
parent 9fa0abe85d
commit 9e80f3dfdd
3 changed files with 6 additions and 11 deletions

View file

@ -121,7 +121,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]`.
This makes it possible to extract Python union types like `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
@ -171,7 +171,7 @@ enum RustyEnum {
```
If the input is neither a string nor an integer, the error message will be:
`"'<INPUT_TYPE>' cannot be converted to 'Union[str, int]'"`.
`"'<INPUT_TYPE>' cannot be converted to 'str | int'"`.
#### `#[derive(FromPyObject)]` Container Attributes
- `pyo3(transparent)`

View file

@ -69,16 +69,11 @@ impl<'a> Enum<'a> {
);
var_extracts.push(ext);
error_names.push_str(&var.err_name);
if i < self.variants.len() - 1 {
error_names.push_str(", ");
if i > 0 {
error_names.push_str(" | ");
}
error_names.push_str(&var.err_name);
}
let error_names = if self.variants.len() > 1 {
format!("Union[{}]", error_names)
} else {
error_names
};
let ty_name = self.enum_ident.to_string();
quote!(
let mut err_reasons = ::std::string::String::new();

View file

@ -409,7 +409,7 @@ fn test_err_rename() {
assert!(f.is_err());
assert_eq!(
f.unwrap_err().to_string(),
"TypeError: failed to extract enum Bar (\'Union[str, uint, int]\')\n- variant A (str): \
"TypeError: failed to extract enum Bar (\'str | uint | int\')\n- variant A (str): \
\'dict\' object cannot be converted to \'PyString\'\n- variant B (uint): \'dict\' object \
cannot be interpreted as an integer\n- variant C (int): \'dict\' object cannot be \
interpreted as an integer\n"