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: 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 `"'<INPUT_TYPE>' cannot be converted to 'Union[str, int]'"`.
`repr()` of the input object.
#### `#[derive(FromPyObject)]` Container Attributes #### `#[derive(FromPyObject)]` Container Attributes
- `pyo3(transparent)` - `pyo3(transparent)`

View File

@ -73,11 +73,7 @@ impl<'a> Enum<'a> {
quote!( quote!(
#(#var_extracts)* #(#var_extracts)*
let type_name = obj.get_type().name(); let type_name = obj.get_type().name();
let from = obj let err_msg = format!("'{}' object cannot be converted to '{}'", type_name, #error_names);
.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);
Err(::pyo3::exceptions::PyTypeError::new_err(err_msg)) 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> { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!( write!(
f, f,
"Can't convert {} to {}", "'{}' object cannot be converted to '{}'",
self.from self.from.get_type().name(),
.repr()
.map(|s| s.to_string_lossy())
.unwrap_or_else(|_| self.from.get_type().name()),
self.to self.to
) )
} }

View File

@ -296,6 +296,6 @@ fn test_err_rename() {
assert!(f.is_err()); assert!(f.is_err());
assert_eq!( assert_eq!(
f.unwrap_err().to_string(), 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,
"conversion_error(None, None, None, None)", "conversion_error(None, None, None, None)",
PyTypeError, PyTypeError,
"argument 'str_arg': Can't convert None to PyString" "argument 'str_arg': 'NoneType' object cannot be converted to 'PyString'"
); );
py_expect_exception!( py_expect_exception!(
py, py,
conversion_error, conversion_error,
"conversion_error(100, None, None, None)", "conversion_error(100, None, None, None)",
PyTypeError, PyTypeError,
"argument 'str_arg': Can't convert 100 to PyString" "argument 'str_arg': 'int' object cannot be converted to 'PyString'"
); );
py_expect_exception!( py_expect_exception!(
py, py,
@ -160,7 +160,7 @@ fn test_conversion_error() {
conversion_error, conversion_error,
"conversion_error('string1', -100, 'string2', None)", "conversion_error('string1', -100, 'string2', None)",
PyTypeError, PyTypeError,
"argument 'tuple_arg': Can't convert 'string2' to PyTuple" "argument 'tuple_arg': 'str' object cannot be converted to 'PyTuple'"
); );
py_expect_exception!( py_expect_exception!(
py, py,