[BUG]: py::args can not be combined with kw_only(), triggers static assert
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.12.0
Problem description
I am defining a method that takes py::args and needs to also accept be able to keyword arguments
I don't want to accept just any keyword argument so I'm not using py::kwargs
The static assert seems to have a problem with this
Reproducible example code
m.def(
"project",
[](const py::object &df, const py::args &args, const string &groups) {
return true;
},
py::arg("df"), py::arg("args"), py::kw_only(), py::arg("kw1") = "");
lib/python3.12/site-packages/pybind11/include/pybind11/pybind11.h:261:9: error: static assertion failed due to requirement 'expected_num_args<pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::kw_only, pybind11::arg_v>(sizeof...(Args), argument_loader<const py::object &, const pybind11::args &, const std::string &>::args_pos >= 0, argument_loader<const py::object &, const pybind11::args &, const std::string &>::has_kwargs)': The number of argument annotations does not match the number of function arguments
static_assert(
^
lib/python3.12/site-packages/pybind11/include/pybind11/pybind11.h:145:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<(lambda at source.cpp:818:6), std::unique_ptr<duckdb::DuckDBPyRelation>, const py::object &, const pybind11::args &, const std::string &, pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::kw_only, pybind11::arg_v>' requested here
initialize(
^
lib/python3.12/site-packages/pybind11/include/pybind11/pybind11.h:1220:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<(lambda at source.cpp:818:6), pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg, pybind11::arg, pybind11::kw_only, pybind11::arg_v, void>' requested here
cpp_function func(std::forward<Func>(f),
^
source.cpp:816:4: note: in instantiation of function template specialization 'pybind11::module_::def<(lambda at source.cpp:818:6), pybind11::arg, pybind11::arg, pybind11::kw_only, pybind11::arg_v>' requested here
m.def(
^
### 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 example using m.def with py::args, py::kw_only(), and the kw1 annotation, then inspect pybind11.h where the static assertion is triggered. Confirm the behavior and determine what test coverage is needed so this combination no longer reports a mismatch while preserving the intended keyword-argument restrictions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100