Skip to content

fix(native_enum): report duplicate value names clearly - #6216

Closed
ViderStar wants to merge 2 commits into
pybind:masterfrom
ViderStar:native-enum-duplicate-value
Closed

ViderStar wants to merge 2 commits into
pybind:masterfrom
ViderStar:native-enum-duplicate-value

Conversation

@ViderStar

Copy link
Copy Markdown

Description

Fixes #5853.

Calling .value() twice with the same name on a py::native_enum is accepted, and the error only shows up in finalize(), where enum.Enum(...) raises TypeError: 'x' already defined as 0. The message does not say which enum is affected, and for a module it ends up chained under ImportError: initialization failed, which is what the reporter saw (they had to bisect their bindings to find the duplicate).

native_enum::value() now checks the names added so far and fails right away with a message in the same format as the existing native_enum errors:

pybind11::native_enum<...>("StandardIconType").value("TitleBarMenuButton"): a member with that name is already defined

Since it is a std::runtime_error, a failure during module init is reported directly as ImportError: <that message>.

Added test_native_enum_duplicate_value_name. Without the change it fails with the TypeError from the enum module. The full test suite passes locally (macOS arm64, Python 3.13, Debug).

Suggested changelog entry:

  • py::native_enum::value() now reports a duplicate member name with a clear error message instead of failing later in finalize() with a TypeError from the enum module.

Adding two values with the same name to a native_enum used to fail only
in finalize(), with a TypeError from the enum module that does not name
the enum. Check for duplicates in value() and fail with a message that
names the enum and the value, like the other native_enum errors.
Use the same make_tuple() conversion as before for the member name, so
that a malformed UTF-8 name still raises UnicodeDecodeError on GraalPy.
@ViderStar

Copy link
Copy Markdown
Author

Closing this — on reflection the change is too minor to be worth your review time. Thanks!

@ViderStar ViderStar closed this Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Misleading errors on duplicate enum key registration

1 participant