Change wording of PyDowncastError display implementation

Displays type(obj) instead of repr(obj) and uses `cannot` instead of
`can't`
to be more consistent with existing python error messages.

See discussion at #1212.
This commit is contained in:
Askaholic 2020-10-13 13:40:03 -08:00
parent 19889bc6b9
commit 6724783395
No known key found for this signature in database
GPG Key ID: 9EA6CC37362892A1
5 changed files with 8 additions and 16 deletions

View File

@ -171,8 +171,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.
`"'<INPUT_TYPE>' cannot be converted to 'Union[str, int]'"`.
#### `#[derive(FromPyObject)]` Container Attributes
- `pyo3(transparent)`

View File

@ -73,11 +73,7 @@ impl<'a> Enum<'a> {
quote!(
#(#var_extracts)*
let type_name = obj.get_type().name();
let from = obj
.repr()
.map(|s| format!("{} ({})", s.to_string_lossy(), type_name))
.unwrap_or_else(|_| type_name.to_string());
let err_msg = format!("Can't convert {} to {}", from, #error_names);
let err_msg = format!("'{}' object cannot be converted to '{}'", type_name, #error_names);
Err(::pyo3::exceptions::PyTypeError::new_err(err_msg))
)
}

View File

@ -490,11 +490,8 @@ impl<'a> std::fmt::Display for PyDowncastError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(
f,
"Can't convert {} to {}",
self.from
.repr()
.map(|s| s.to_string_lossy())
.unwrap_or_else(|_| self.from.get_type().name()),
"'{}' object cannot be converted to '{}'",
self.from.get_type().name(),
self.to
)
}

View File

@ -296,6 +296,6 @@ fn test_err_rename() {
assert!(f.is_err());
assert_eq!(
f.unwrap_err().to_string(),
"TypeError: Can't convert {} (dict) to Union[str, uint, int]"
"TypeError: 'dict' object cannot be converted to 'Union[str, uint, int]'"
);
}

View File

@ -139,14 +139,14 @@ fn test_conversion_error() {
conversion_error,
"conversion_error(None, None, None, None)",
PyTypeError,
"argument 'str_arg': Can't convert None to PyString"
"argument 'str_arg': 'NoneType' object cannot be converted to 'PyString'"
);
py_expect_exception!(
py,
conversion_error,
"conversion_error(100, None, None, None)",
PyTypeError,
"argument 'str_arg': Can't convert 100 to PyString"
"argument 'str_arg': 'int' object cannot be converted to 'PyString'"
);
py_expect_exception!(
py,
@ -160,7 +160,7 @@ fn test_conversion_error() {
conversion_error,
"conversion_error('string1', -100, 'string2', None)",
PyTypeError,
"argument 'tuple_arg': Can't convert 'string2' to PyTuple"
"argument 'tuple_arg': 'str' object cannot be converted to 'PyTuple'"
);
py_expect_exception!(
py,