Allow creating tuple and list from iterator range
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Summary
The pybind11 API does not allow creating python tuples whose size is only known at run-time, although the underlying CPython API supports this. Please add this functionality.
Detailed description
The pybind11 API correctly prevent users from changing tuple items, since tuples are supposed to be immutable in Python. To create tuples, pybind11 offers a std::make_tuple-like function
auto tup = pybind11::make_tuple(1, 2, 3);
This is fine if the number of items to be stored is known at compile-time, but this is not always the case and not actually required by the CPython API.
It should also be allowed to make tuple from an iterator range. The idiomatic way to support this would be to add another constructor to pybind11::tuple, so that the following works
auto tup = pybind11::tuple(my_container.begin(), my_container.end());
The tuple is still immutable after construction. The constructor should use pybind11::detail::object_or_cast to convert the arguments.
An analog constructor should then be added to pybind11::list for consistency.
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 locating the pybind11::tuple and pybind11::list constructors, then inspect pybind11::detail::object_or_cast to understand the required conversions. Add iterator-range construction for both types while preserving tuple immutability, and verify the behavior with the project's existing tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api, backend-api-design
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100