[BUG]: "Already defined" import error when reusing a value name in different enums
Open
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
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
v3.0.3
Problem description
native_enum value names seem to have global collision scope. When adding a value with a name which was already used in another enum, it compiles successfully, but an ImportError is raised when importing.
Reproducible example code
#include <pybind11/pybind11.h>
#include <pybind11/native_enum.h>
namespace py = pybind11;
enum class SomeEnum { value, another_value };
enum class AnotherEnum { first_value, second_value, another_value };
PYBIND11_MODULE(enum_collision_test, mod) {
py::native_enum<SomeEnum>(mod, "SomeEnum", "enum.Enum")
.value("VALUE", SomeEnum::value)
// First definition which uses "ANOTHER_VALUE"
.value("ANOTHER_VALUE", SomeEnum::another_value)
.export_values()
.finalize();
py::native_enum<AnotherEnum>(mod, "AnotherEnum", "enum.Enum")
.value("FIRST_VALUE", AnotherEnum::first_value)
.value("SECOND_VALUE", AnotherEnum::second_value)
// Causes ImportError: pybind11::native_enum<...>("AnotherEnum").value("ANOTHER_VALUE"): an object with that name is already defined
.value("ANOTHER_VALUE", AnotherEnum::another_value)
.export_values()
.finalize();
}
Is this a regression? Put the last known working version here if it is.
Not a regression
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 with the native_enum usage in the reproducible C++ example and trace how value names are registered during finalize(). Verify the behavior with two enums that reuse ANOTHER_VALUE, then add coverage for the expected import behavior. Done means distinct enums can reuse a value name without an ImportError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100