pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: Default value for a callback function parameter causes incompatible function arguments error

Open dbs4261 opened this issue 6 months ago • 0 comments

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.

What version (or hash if on master) of pybind11 are you using?

a2e59f0e7065404b44dfe92a28aca47ba1378dc4 # v2.13.6

Problem description

I should be able to provide a default value for a callable argument to a function, particularly because the type hint for a callable is actually an Optional[Callable[...]]. Passing py::None or nullptr should act as that default value.

Reproducible example code

C++ library:
 c++
PYBIND11_MODULE(test_library, m) {
    m.def("temp", [](py::typing::Callable<void(py::int_)> callback) {
        return;
    }, py::arg_v("callback", py::none{}));
}


Python code:
 python
import test_library
test_library.temp()


Error:

TypeError: temp(): incompatible function arguments. The following argument types are supported:
    1. (callback: Callable[[int], None] = None) -> None

Is this a regression? Put the last known working version here if it is.

Not a regression

dbs4261 avatar Jun 11 '25 16:06 dbs4261