sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
232 stars 47 forks source link

WARNING - Generated stubs signature is degraded to `(*args, **kwargs) -> typing.Any` for #83

Closed nyckmaia closed 2 years ago

nyckmaia commented 2 years ago

Hi, I got a perfect running stubs generation using pybind11-stubgen. It works great.

Question

Now, I just would like to remove this warning below. Could you help me?

[2022-08-18 20:57:31,607] {__init__.py:131} WARNING - Generated stubs signature is degraded to `(*args, **kwargs) -> typing.Any` for
[2022-08-18 20:57:31,607] {__init__.py:135} WARNING - def __init__(self: maialib.maiacore.Score, partsName: std::initializer_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, numMeasures: int = 20) -> None: ...
[2022-08-18 20:57:31,607] {__init__.py:136} WARNING -                                                          ^-- Invalid syntax
[2022-08-18 20:57:31,672] {__init__.py:957} INFO - Useful link: Avoiding C++ types in docstrings:
[2022-08-18 20:57:31,673] {__init__.py:958} INFO -       https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-cpp-types-in-docstrings
/home/nyck/.local/lib/python3.8/site-packages/coverage/control.py:794: CoverageWarning: No data was collected. (no-data-collected)
  self._warn("No data was collected.", slug="no-data-collected")

Here the same message in a print screen

image

Here is my Score class bind code

    // bindings to Score class
    py::class_<Score> cls(m, "Score");
    cls.def(py::init<const std::initializer_list<std::string>&, const int>(),
            py::arg("partsName"),
            py::arg("numMeasures") = 20,
            py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>());

    cls.def(py::init<const std::vector<std::string>&, const int>(),
            py::arg("partsName"),
            py::arg("numMeasures") = 20,
            py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>());

    cls.def(py::init<const std::string&>(),
            py::arg("filePath"),
            py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>());

Here the same code in a print screen (with colors):

image

Thank you

sizmailov commented 2 years ago

You have a type which is not known to pybind, therefore it renders as C++. Please follow the link from the message (https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-cpp-types-in-docstrings) for more details.

nyckmaia commented 2 years ago

@sizmailov this warning message is related to my use of const std::initializer_list<std::string>& in this constructor overload below?

py::class_<Score> cls(m, "Score");

cls.def(py::init<const std::initializer_list<std::string>&, const int>(),
            py::arg("partsName"),
            py::arg("numMeasures") = 20,
            py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>());

This is not a custom type. So, I don't know how to solve it.

Could you help me?

Thank you

sizmailov commented 2 years ago

Yes, the problem is with std::initializer_list<std::string> constructor overload as you can see from the error message, note the ^-- Invalid syntax part:

[2022-08-18 20:57:31,607] {__init__.py:135} WARNING - def __init__(self: maialib.maiacore.Score, partsName: std::initializer_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, numMeasures: int = 20) -> None: ...
[2022-08-18 20:57:31,607] {__init__.py:136} WARNING -                                                          ^-- Invalid syntax

The problem is that pybind module needs to know bindings of the type. It doesn't matter whether a C++ type comes from the STL or not. Pybind provides bindings for some classes from STL, but if you need some beyond that you must provide bindings by yourself.