pybind / pybind/pybind11

[FEAT] Automatic conversions for C++ type-aliases.

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

Nobody has claimed this yet.

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

Description

I have a bunch of type aliases in my library, which alias std::string and int. For example:

using SomeName = std::string;

These are used as arguments or return types in classes and functions. For example:

class SomeClass {
public:
  SomeClass(SomeName name) {}
};

When I bind the class above using pybind11, the Python interpreter doesn't recognise SomeName as a new type, but binds it to a Python string instead (see the two snippets below).

py::class_<SomeClass>(m, "SomeClass")
  .def(py::init<SomeName>();
>> help(SomeClass)
Help on class SomeClass in module some_module:

class SomeClass(pybind11_builtins.pybind11_object)
...
 |  Methods defined here:
 |  __init__(...)
 |      __init__(self: some_module.SomeClass, arg0: str) -> None
                                                    ~~~
...

It's quite easy to create aliases in Python (e.g. SomeName = str). Is it possible to add a feature that automatically creates type-aliases in Python, based on the c++ type-aliases, and show the correct type names in the method description?

I considered using custom type casters, but that doesn't allow me to cast two (or more) aliases that alias the same type.

Discussion started on Gitter here and continued here.

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

The issue does not name any files, tests, or implementation entry points. Begin by reviewing how pybind11 exposes C++ argument and return types to Python, then define how aliases should be represented and verified in generated help output.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.