pybind11
pybind11 copied to clipboard
[BUG]: Stubs are not generated for callback functions
Required prerequisites
- [X] Make sure you've read the documentation. Your issue may be addressed there.
- [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- [ ] Consider asking first in the Gitter chat room or in a Discussion.
Problem description
Stubs are not generated for callback functions.
[2022-09-08 12:06:08,848] {__init__.py:131} WARNING - Generated stubs signature is degraded to `(*args, **kwargs) -> typing.Any` for
[2022-09-08 12:06:08,849] {__init__.py:135} WARNING - def test_callback3(arg0: std::function<int __cdecl(int)>)
-> str: ...
[2022-09-08 12:06:08,850] {__init__.py:136} WARNING - ^-- Invalid syntax
Code example tested was the pybind11 example in tests/test_callbacks.cpp
Reproducible example code
m.def("test_callback3", [](const std::function<int(int)> &func) {
return "func(43) = " + std::to_string(func(43));
});
@benknight135 If you want to give a shot at fixing this, you'll need a special template handler for generating the proper type string from the std::function. It would need to be added here: https://github.com/pybind/pybind11/blob/59f03ee389c283cde65bd800c8f32ea690daf3fd/include/pybind11/cast.h#L905 .
Actually, weird. It should generate the type caster in the proper function here: https://github.com/pybind/pybind11/blob/aa8f8baa4e402885e5c89f5dc42a579932c33790/include/pybind11/functional.h#L124
I am unable to reproduce the unit test I wrote for this passes:
def test_callback_doc(doc):
assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
@benknight135
Closing since there isn't a reproducer and we could not repro it.