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 d84720e commit 01e22f0
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 @@ -21,7 +21,7 @@ fn test_conversion_error() {
conversion_error,
"conversion_error('100, -100)",
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 Down Expand Up @@ -49,14 +49,14 @@ fn test_conversion_error_signature() {
conversion_error_signature,
"conversion_error_signature('a string', 'another string')",
PyTypeError,
"argument 'arg1': Can't convert 'a string' to PyTuple"
"argument 'arg1': 'str' object cannot be conterted to 'PyTuple'"
);
py_expect_exception!(
py,
conversion_error_signature,
"conversion_error_signature('100, -100)",
PyTypeError,
"argument 'arg2': Can't convert '-100' to Option<PyLong>"
"argument 'arg2': 'int' object cannot be converted to 'Option<PyLong>'"
);
}

Expand Down

0 comments on commit 01e22f0

Please sign in to comment.