Skip to content

Commit

Permalink
Change wording of PyDowncastError display implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Askaholic committed Oct 13, 2020
1 parent 8d02d12 commit 31b64cd
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 16 deletions.
3 changes: 1 addition & 2 deletions guide/src/conversions/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
6 changes: 1 addition & 5 deletions pyo3-derive-backend/src/from_pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
}
Expand Down
7 changes: 2 additions & 5 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]'"
);
}
6 changes: 3 additions & 3 deletions tests/test_pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 31b64cd

Please sign in to comment.