pybind / pybind/pybind11

[BUG]: Conflict when binding two classes with same name from different anonymous namespaces

Open
#3,289 9 comments 0 reactions 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
Problem description

I have two separate C++ files, each of which defines a class with the same name in an anonymous namespace (thus "private" to that file) and then binds that class with pybind. Each of these files provides a different Python module name. Hence, in my mind, there should be no conflict: two separate modules, two separate files, two separate classes belonging to two separate anonymous namespaces.

However, when I import both modules, this happens upon importing the second one:

ImportError: generic_type: type "MyClass" is already registered!

Which makes it seem that pybind is getting confused and somehow mixes up these two classes because they have the same name, even though they're completely different C++ types.

Reproducible example code

https://gist.github.com/lw/b7f14d4749b2c0e010d8db893d3928f5

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 reproducible example code in the linked gist and import both generated Python modules to confirm the registration conflict. Trace how pybind11 identifies and registers the two C++ classes, then verify that the modules can be imported together without treating the distinct anonymous-namespace types as the same type.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.