mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-11 17:00:34 +00:00
Make TypeErrors more informative when an optional header is missing
E.g. trying to convert a `list` to a `std::vector<int>` without including <pybind11/stl.h> will now raise an error with a note that suggests checking the headers. The note is only appended if `std::` is found in the function signature. This should only be the case when a header is missing. E.g. when stl.h is included, the signature would contain `List[int]` instead of `std::vector<int>` while using stl_bind.h would produce something like `MyVector`. Similarly for `std::map`/`Dict`, `complex`, `std::function`/`Callable`, etc. There's a possibility for false positives, but it's pretty low.
This commit is contained in:
@@ -694,6 +694,16 @@ protected:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto append_note_if_missing_header_is_suspected = [](std::string &msg) {
|
||||
if (msg.find("std::") != std::string::npos) {
|
||||
msg += "\n\n"
|
||||
"Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
|
||||
"<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
|
||||
"conversions are optional and require extra headers to be included\n"
|
||||
"when compiling your pybind11 module.";
|
||||
}
|
||||
};
|
||||
|
||||
if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
|
||||
if (overloads->is_operator)
|
||||
return handle(Py_NotImplemented).inc_ref().ptr();
|
||||
@@ -751,12 +761,14 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
append_note_if_missing_header_is_suspected(msg);
|
||||
PyErr_SetString(PyExc_TypeError, msg.c_str());
|
||||
return nullptr;
|
||||
} else if (!result) {
|
||||
std::string msg = "Unable to convert function return value to a "
|
||||
"Python type! The signature was\n\t";
|
||||
msg += it->signature;
|
||||
append_note_if_missing_header_is_suspected(msg);
|
||||
PyErr_SetString(PyExc_TypeError, msg.c_str());
|
||||
return nullptr;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user