pybind / pybind/pybind11

[ENH]: Make Collections Casters more customizable.

Open
#3,892 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

casters enhancement
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
Problem description

We need a better way to customize the return type of functions. Specifically, we need a way to control the types of casting behavior of nested collections AND have it be function specific. There are use cases where instead of converting to unicode strings, we may want to convert to bytes, or we may want to auto-freeze all sets into frozen sets etc.

For simple use objects, we can just wrap them in the appropiate python types or do the casting in a lambda wrapper, but doing this at scale proves difficult. See the lengthy discussion on 3838. Adding more, return type flags is a non-scalable way of solving this problem.

Currently, all the list, set, etc casters call make_casters on their keys or values to get the casters needed for the function. All we need to do is a templated_way to override that make_caster template with our own templated derivatives. Specifically, the user should be able to specify the way of selecting the caster.

See an example here: https://github.com/pybind/pybind11/blob/c4e295287b74c0b3f0f8a304cdb746ac2bcb37ad/include/pybind11/stl.h#L143

Specifically, this can be fixed if we add more optional template parameters to make_caster in a optional backwards compatible way. Specifically, we need a way to define a custom make_caster recursively for all list, set, and map types. Adding this type of hook would solve most of the issues with casting collections, the user would just call their own custom make_caster which could forward optional args to list, set, map, etc casters.

To make this painless, we would make list_caster, set_caster, map_caster etc more customizable and allow users to specify their own custom make_caster template for the key and value converters.

Thoughts @rwgk @henryiii @wangxf123456 ?

Reproducible example code

No response

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 with the collection caster implementation in include/pybind11/stl.h, especially the make_caster example around line 143, and review the linked discussion on issue 3838. Define the customization hook and its recursive behavior for list, set, and map casters; done means a backwards-compatible design and implementation with coverage for function-specific nested collection casting.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.