From e99ebaedcf5944a80e41313dcdc56e89966e232b Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Mon, 12 Sep 2016 11:44:37 +0900 Subject: [PATCH] nicer error message for invalid function arguments --- include/pybind11/pybind11.h | 8 +++++--- tests/test_callbacks.py | 2 +- tests/test_inheritance.py | 5 +++-- tests/test_issues.py | 15 +++++++++------ tests/test_kwargs_and_defaults.py | 5 +++-- tests/test_opaque_types.py | 5 +++-- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 1468467be..b602439b7 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -460,8 +460,10 @@ protected: if (overloads->is_operator) return handle(Py_NotImplemented).inc_ref().ptr(); - std::string msg = "Incompatible " + std::string(overloads->is_constructor ? "constructor" : "function") + - " arguments. The following argument types are supported:\n"; + std::string msg = std::string(overloads->name) + "(): incompatible " + + std::string(overloads->is_constructor ? "constructor" : "function") + + " arguments. The following argument types are supported:\n"; + int ctr = 0; for (detail::function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) { msg += " "+ std::to_string(++ctr) + ". "; @@ -489,7 +491,7 @@ protected: msg += "\n"; } - msg += " Invoked with: "; + msg += "\nInvoked with: "; tuple args_(args, true); for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { msg += static_cast(static_cast(args_[ti]).str()); diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 8f867d43a..c2668aa95 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -83,7 +83,7 @@ def test_cpp_function_roundtrip(): with pytest.raises(TypeError) as excinfo: test_dummy_function(dummy_function2) - assert "Incompatible function arguments" in str(excinfo.value) + assert "incompatible function arguments" in str(excinfo.value) with pytest.raises(TypeError) as excinfo: test_dummy_function(lambda x, y: x + y) diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py index d6997b09c..351fe6b2c 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -24,9 +24,10 @@ def test_inheritance(msg): with pytest.raises(TypeError) as excinfo: dog_bark(polly) assert msg(excinfo.value) == """ - Incompatible function arguments. The following argument types are supported: + dog_bark(): incompatible function arguments. The following argument types are supported: 1. (arg0: m.Dog) -> str - Invoked with: + + Invoked with: """ diff --git a/tests/test_issues.py b/tests/test_issues.py index ad3d39d51..e2ab0b45c 100644 --- a/tests/test_issues.py +++ b/tests/test_issues.py @@ -65,17 +65,19 @@ def test_no_id(capture, msg): with pytest.raises(TypeError) as excinfo: get_element(None) assert msg(excinfo.value) == """ - Incompatible function arguments. The following argument types are supported: + get_element(): incompatible function arguments. The following argument types are supported: 1. (arg0: m.issues.ElementA) -> int - Invoked with: None + + Invoked with: None """ with pytest.raises(TypeError) as excinfo: expect_int(5.2) assert msg(excinfo.value) == """ - Incompatible function arguments. The following argument types are supported: + expect_int(): incompatible function arguments. The following argument types are supported: 1. (arg0: int) -> int - Invoked with: 5.2 + + Invoked with: 5.2 """ assert expect_float(12) == 12 @@ -90,10 +92,11 @@ def test_str_issue(msg): with pytest.raises(TypeError) as excinfo: str(StrIssue("no", "such", "constructor")) assert msg(excinfo.value) == """ - Incompatible constructor arguments. The following argument types are supported: + __init__(): incompatible constructor arguments. The following argument types are supported: 1. m.issues.StrIssue(arg0: int) 2. m.issues.StrIssue() - Invoked with: no, such, constructor + + Invoked with: no, such, constructor """ diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 0e1ea805e..852d03c6e 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -35,9 +35,10 @@ def test_named_arguments(msg): # noinspection PyArgumentList kw_func2(x=5, y=10, z=12) assert msg(excinfo.value) == """ - Incompatible function arguments. The following argument types are supported: + kw_func2(): incompatible function arguments. The following argument types are supported: 1. (x: int=100, y: int=200) -> str - Invoked with: + + Invoked with: """ assert kw_func4() == "{13 17}" diff --git a/tests/test_opaque_types.py b/tests/test_opaque_types.py index 943686383..8a6a4c3f3 100644 --- a/tests/test_opaque_types.py +++ b/tests/test_opaque_types.py @@ -35,9 +35,10 @@ def test_pointers(msg): with pytest.raises(TypeError) as excinfo: get_void_ptr_value([1, 2, 3]) # This should not work assert msg(excinfo.value) == """ - Incompatible function arguments. The following argument types are supported: + get_void_ptr_value(): incompatible function arguments. The following argument types are supported: 1. (arg0: capsule) -> int - Invoked with: [1, 2, 3] + + Invoked with: [1, 2, 3] """ assert return_null_str() is None