Converting recursive types
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
I have the following recursive type:
struct node_t;
typedef std::variant<int, float, std::string, node_t, std::vector<node_t>> value_t;
struct node_t : public std::map<std::string, value_t> {};
with the following custom type caster
namespace pybind11 { namespace detail {
template <> struct type_caster<node_t>
{
PYBIND11_TYPE_CASTER(node_t, _("node_t"));
static handle
cast(value_t&& value, return_value_policy policy, handle parent)
{
return std::visit([policy, parent](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, node_t>)
{
dict d;
for (auto&& kv : arg)
{
auto key = reinterpret_steal<object>(PyUnicode_DecodeLatin1(kv.first.data(), kv.first.length(), NULL));
auto value = reinterpret_steal<object>(make_caster<value_t>::cast(forward_like<value_t>(kv.second), policy, parent));
if (!key or !value)
return handle();
d[key] = value;
}
return d.release();
}
else if constexpr (std::is_same_v<T, std::vector<node_t>>)
{
list l(arg.size());
size_t index = 0;
for (auto&& element : arg)
{
auto value = reinterpret_steal<object>(make_caster<node_t>::cast(forward_like<node_t>(element), policy, parent));
if (!value)
return handle();
PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value.release().ptr()); // steals a reference
}
return l.release();
}
else if constexpr (std::is_same_v<T, std::string>)
return handle(PyUnicode_DecodeLatin1(arg.data(), arg.length(), NULL));
else
return make_caster<T>::cast(arg, policy, parent);
}, value);
}
};
}}
Why is it not possible to replace the map and list caster with a call to either map_caster or list_caster as defined in the header file <pybind11/stl.h> where I also borrowed the code for my list and map caster?
For example I want to call return list_caster<T, node_t>::cast(arg) instead of
list l(arg.size());
size_t index = 0;
for (auto&& element : arg)
{
auto value = reinterpret_steal<object>(make_caster<node_t>::cast(forward_like<node_t>(element), policy, parent));
if (!value)
return handle();
PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value.release().ptr()); // steals a reference
}
return l.release();
but I always get the error message
error: no matching function for call to 'pybind11::detail::list_caster<std::vector<node_t>, node_t>::cast(std::vector<node_t>&)'
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 reading pybind11/stl.h, especially the map_caster and list_caster definitions and their template parameters. Reproduce the reported call with the recursive node_t and value_t types, then compare the compiler's argument deduction with the caster signatures. Done means determining whether these casters can support the recursive type and documenting the applicable usage or limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100