Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TypeErrors more informative when an optional header is missing #1077

Merged
merged 1 commit into from
Sep 12, 2017

Conversation

dean0x7d
Copy link
Member

One of the most frequent questions, both in the issues and Gitter, is: "Why doesn't the std::vector<T> conversion work?" The answer is usually: missing #include <pybind11/stl.h>.

As far as I know, there is no way to diagnose this issue at compile time (at least not while also keeping all the dependencies optional), but this PR attempts to add an informative message to conversion errors which are likely caused by missing 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.

The full error message looks something like this:

TypeError: missing_header_arg(): incompatible function arguments. The following argument types are supported:
    1. (arg0: std::vector<float, std::allocator<float> >) -> None

Invoked with: [1.0, 2.0, 3.0]

Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,
<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic
conversions are optional and require extra headers to be included
when compiling your pybind11 module.

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.
@dean0x7d dean0x7d added this to the v2.2.1 milestone Sep 10, 2017
@dean0x7d dean0x7d merged commit 2b4477e into pybind:master Sep 12, 2017
@dean0x7d dean0x7d deleted the header-message branch September 12, 2017 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant