pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.11k stars 2.05k forks source link

[BUG]: `kw_only()` at start of one overload of function causes cast to be more strict #4866

Open crisluengo opened 9 months ago

crisluengo commented 9 months ago

Required prerequisites

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

master (0a756c0b)

Problem description

This is a rather strange issue in that any change to the code below causes the issue to not show up.

We define a function with two overloads, one to be called as test_cast(v), and one as test_cast(x=x, v=v), where v is a std::vector<double>. This requires in Python that v be a list of floats, but [5,6,7] is accepted just fine (as it should!).

However, for the second overload, v=[5,6,7] throws an error:

E       TypeError: test_cast(): incompatible function arguments. The following argument types are supported:
E           1. (v: list[float]) -> list[float]
E           2. (*, x: float, v: list[float]) -> list[float]
E       
E       Invoked with: kwargs: x=1.0, v=[5, 6, 7]

This error only happens when kw_only() comes before the first argument in the second overload. If we move it to the second position, all is well again.

I've contributed large fixes to Pybind11 before, but in this case I don't know where to start searching for the error.

To reproduce the error:

This can be added to any of the existing test files to reproduce the problem.

In C++:

m.def(
    "test_cast",
    [](std::vector<double> const& v) { return v; },
    "v"_a);

m.def(
    "test_cast",
    [](double, std::vector<double> const& v) { return v; },
    py::kw_only(),
    "x"_a,
    "v"_a);

In Python:

def test_cast_kw():
    m.test_cast(v=[5.0, 6.0, 7.0])         # ok
    m.test_cast(x=1.0, v=[5.0, 6.0, 7.0])  # ok
    m.test_cast(v=[5, 6, 7])               # ok
    m.test_cast(x=1.0, v=[5, 6, 7])        # throws

Reproducible example code

// C++:

m.def(
    "test_cast",
    [](std::vector<double> const& v) { return v; },
    "v"_a);

m.def(
    "test_cast",
    [](double, std::vector<double> const& v) { return v; },
    py::kw_only(),
    "x"_a,
    "v"_a);

# Python:

def test_cast_kw():
    m.test_cast(v=[5.0, 6.0, 7.0])         # ok
    m.test_cast(x=1.0, v=[5.0, 6.0, 7.0])  # ok
    m.test_cast(v=[5, 6, 7])               # ok
    m.test_cast(x=1.0, v=[5, 6, 7])        # throws

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

Not now if a regression