From 6724783395ddc3e85d2a223be7257ab108df4ac3 Mon Sep 17 00:00:00 2001 From: Askaholic Date: Tue, 13 Oct 2020 13:40:03 -0800 Subject: [PATCH] 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. --- guide/src/conversions/traits.md | 3 +-- pyo3-derive-backend/src/from_pyobject.rs | 6 +----- src/err/mod.rs | 7 ++----- tests/test_frompyobject.rs | 2 +- tests/test_pyfunction.rs | 6 +++--- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/guide/src/conversions/traits.md b/guide/src/conversions/traits.md index 7d997538..d09a684f 100644 --- a/guide/src/conversions/traits.md +++ b/guide/src/conversions/traits.md @@ -171,8 +171,7 @@ enum RustyEnum { ``` If the input is neither a string nor an integer, the error message will be: -`"Can't convert to Union[str, int]"`, where `` is replaced by the type name and -`repr()` of the input object. +`"'' cannot be converted to 'Union[str, int]'"`. #### `#[derive(FromPyObject)]` Container Attributes - `pyo3(transparent)` diff --git a/pyo3-derive-backend/src/from_pyobject.rs b/pyo3-derive-backend/src/from_pyobject.rs index 8d8dc956..5ea0754d 100644 --- a/pyo3-derive-backend/src/from_pyobject.rs +++ b/pyo3-derive-backend/src/from_pyobject.rs @@ -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)) ) } diff --git a/src/err/mod.rs b/src/err/mod.rs index 294cbe9b..461a1ad4 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -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 ) } diff --git a/tests/test_frompyobject.rs b/tests/test_frompyobject.rs index 98f2d2b9..24a7322b 100644 --- a/tests/test_frompyobject.rs +++ b/tests/test_frompyobject.rs @@ -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]'" ); } diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index affb768a..1e088955 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -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,