pybind / pybind/pybind11

[FEATURE REQUEST]: Dynamic class names (`pybind11::class_` constructor taking `std::string`)

Open
#4,777 2 comments 1 reaction 0 assignees View on GitHub

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
What version (or hash if on master) of pybind11 are you using?

2.10.4

Problem description

I often find myself wrapping C++ templates using pybind11. In this case, I can write a template function that instantiates and registers a specific form of the template. Of course, if I do this for multiple instantiations, I end up with multiple python classes, and those should have distinct names. I would like to generate these names in the template function at runtime. The problem is that the class_ constructors only take const char * for the name, providing no way of managing the dynamic memory where the runtime-generated name resides (a naive use of std::string::c_str() obviously leads to dangling references).

Python's type system probably wasn't designed with C++ templates in mind, so it likely doesn't provide an direct way of resource management for the name of a type object. However, we can make a workaround using weak references, following the example given in the pybind11 documentation. I have been using the following:

template <typename Cls, typename... Options, typename... Args>
auto make_class(py::module_ &m, std::string name, Args &&...args) {
  auto holder = std::make_unique<std::string>(std::move(name));
  auto ret = py::class_<Cls, Options...>(m, holder->c_str(), std::forward<Args>(args)...);
  py::cpp_function cleanup_callback([holder = std::move(holder)](py::handle weakref) mutable {
    holder.reset();
    weakref.dec_ref();
  });
  (void)pybind11::weakref(ret, cleanup_callback).release();
  return ret;
}

I wonder if something like this could be reasonably included in a dedicated constructor?

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

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 by reviewing the pybind11::class_ constructors and the weak-reference workaround shown in the issue. Determine how a std::string-based constructor could preserve the dynamic class name safely, then add coverage for the constructor and its name lifetime before documenting the supported usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.