mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +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:
@@ -179,3 +179,22 @@ def test_stl_pass_by_pointer(msg):
|
||||
""" # noqa: E501 line too long
|
||||
|
||||
assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
|
||||
|
||||
|
||||
def test_missing_header_message():
|
||||
"""Trying convert `list` to a `std::vector`, or vice versa, without including
|
||||
<pybind11/stl.h> should result in a helpful suggestion in the error message"""
|
||||
import pybind11_cross_module_tests as cm
|
||||
|
||||
expected_message = ("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.")
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
cm.missing_header_arg([1.0, 2.0, 3.0])
|
||||
assert expected_message in str(excinfo.value)
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
cm.missing_header_return()
|
||||
assert expected_message in str(excinfo.value)
|
||||
|
||||
Reference in New Issue
Block a user