[BUG]: default value for Custom type casters not taken into count
Open
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
- Make sure you've read the documentation. Your issue may be addressed there.
- 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?
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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