pybind / pybind/pybind11

[QUESTION] disambiguating lambda arguments in function overloads

Open
#3,035 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I have a question about writing overloaded function bindings and I could not find the answer anywhere in the docs or FAQ.

In my C++ code I have two overloaded variants of a function (call it "func"), each of which takes a single argument that is a std::function instance (lambda function). When I run my code in pure C++, disambiguation of the two argument types works just fine. However, when I try to write a pybind11 python module using the py::overload_cast tool, the disambiguation fails and I get a TypeError. Here is a simplified demonstration of the issue I'm running into. I am using C++14.

C++ source:

#include <pybind11/pybind11.h>
#include <pybind11/functional.h>

namespace py = pybind11;

int func(const std::function<int(int)> &f) { return f(10); }

int func(const std::function<int(int,int)> &f) { return f(10,4); }

PYBIND11_MODULE(mymodule, m) {
    m.def("func", py::overload_cast<const std::function<int(int)> &>(&func));
    m.def("func", py::overload_cast<const std::function<int(int,int)> &>(&func));
}

Python source:

import mymodule

def f1(a):
    return 2*a

def f2(a, b):
    return 2*a + b

print(mymodule.func(f1))
print(mymodule.func(f2))

Python output:

20
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print(mymodule.func(f2))
TypeError: f2() missing 1 required positional argument: 'b'

Is there a way to get the desired overloading behavior with pybind11? I've tried adding type hints to the f1 and f2 python functions, thinking perhaps this would help pybind11 disambiguate the different lambda arguments. No luck.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the PYBIND11_MODULE binding definitions using py::overload_cast and the included pybind11/functional.h support, then reproduce the f1 and f2 calls from the Python example. Compare how the two std::function overloads are selected and determine whether the desired behavior is supported; done means documenting or validating the appropriate approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.