Skip to content

Commit be6f5e9

Browse files
committed
Add Type to bad args output
The changes the error message for invalid arguments to display 'Type (= strvalue)' instead of just 'strvalue' for the given argument list. Fixes #517.
1 parent c4d8196 commit be6f5e9

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

include/pybind11/pybind11.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,13 @@ class cpp_function : public function {
509509
msg += "\nInvoked with: ";
510510
auto args_ = reinterpret_borrow<tuple>(args);
511511
for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
512-
msg += static_cast<std::string>(pybind11::str(args_[ti]));
512+
auto pytype = args_[ti].get_type();
513+
pybind11::str value(args_[ti]);
514+
msg += static_cast<std::string>(
515+
hasattr(pytype, "__name__")
516+
? pybind11::str("{} (= {})").format(getattr(pytype, "__name__"), value)
517+
: value);
518+
513519
if ((ti + 1) != args_.size() )
514520
msg += ", ";
515521
}

tests/test_inheritance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_inheritance(msg):
2727
dog_bark(): incompatible function arguments. The following argument types are supported:
2828
1. (arg0: m.Dog) -> str
2929
30-
Invoked with: <m.Pet object at 0>
30+
Invoked with: Pet (= <m.Pet object at 0>)
3131
"""
3232

3333

tests/test_issues.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_no_id(msg):
6969
get_element(): incompatible function arguments. The following argument types are supported:
7070
1. (arg0: m.issues.ElementA) -> int
7171
72-
Invoked with: None
72+
Invoked with: NoneType (= None)
7373
"""
7474

7575
with pytest.raises(TypeError) as excinfo:
@@ -78,7 +78,7 @@ def test_no_id(msg):
7878
expect_int(): incompatible function arguments. The following argument types are supported:
7979
1. (arg0: int) -> int
8080
81-
Invoked with: 5.2
81+
Invoked with: float (= 5.2)
8282
"""
8383
assert expect_float(12) == 12
8484

@@ -96,7 +96,7 @@ def test_str_issue(msg):
9696
1. m.issues.StrIssue(arg0: int)
9797
2. m.issues.StrIssue()
9898
99-
Invoked with: no, such, constructor
99+
Invoked with: str (= no), str (= such), str (= constructor)
100100
"""
101101

102102

tests/test_opaque_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_pointers(msg):
3838
get_void_ptr_value(): incompatible function arguments. The following argument types are supported:
3939
1. (arg0: capsule) -> int
4040
41-
Invoked with: [1, 2, 3]
41+
Invoked with: list (= [1, 2, 3])
4242
"""
4343

4444
assert return_null_str() is None

0 commit comments

Comments
 (0)