pybind11
pybind11 copied to clipboard
[BUG]: `kw_only()` at start of one overload of function causes cast to be more strict
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?
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