pybind / pybind/pybind11

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

Open
#4,866 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

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 provided m.def overloads and the test_cast_kw reproduction, focusing on the overload that places kw_only() before x. Trace how that overload handles integer elements in v compared with the other overload. Done means both calls using v=[5, 6, 7] succeed while preserving the keyword-only behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.