pybind / pybind/pybind11

[BUG]: default value for Custom type casters not taken into count

Open
#4,628 1 comment 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?

2.10.4

Problem description

When creating a Custom type caster for a C++ defined type and then passing it as a function parameter, the default value is not taken into count.

Reproducible example code
I have this function:

void my_functions(int a, some_type b);

And I want to bind it using only the default argument for b:

 m.def("my_functions", &my_functions,  pb::arg("a"),  pb::arg("b") = function_that_returns_my_type()); // just default for b

some_type has an automatic custom type caster declared in PYBIND11_NAMESPACE (!)

This compiles, but when I call in python

my_functions(5)
I get:

TypeError: my_functions(): incompatible function arguments. The following argument types are supported:
    1. (a: int, b: some_type = python_object_custom_casted)
Is this a regression? Put the last known working version here if it is.

Not 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 by reproducing the reported binding with a custom type caster and the call my_functions(5). Inspect pybind11's handling of custom type casters and default arguments, including the PYBIND11_NAMESPACE declaration. Done means the default value for b is accepted when the function is called with only a.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.