Repository navigation
feat: add enum names and values properties - #6314
Open
cielavenir wants to merge 3 commits into
Open
cielavenir wants to merge 3 commits into
cielavenir wants to merge 3 commits into
Conversation
Assisted-by: Claude Code:claude-opus-5-5
Contributor
Author
|
reviewed the pull request description |
cielavenir
marked this pull request as ready for review
November 26, 2026 02:15
Collaborator
|
Quick question: can you use |
Collaborator
|
Oh, sorry, I see these are the pre-standard library enum boost methods, which we don't provide (and shouldn't). Modern code should match The standard way to access these in Python or pybind11 is |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
I'm trying to build our software using stock pybind11.
our patch list:
type.-> trying to resolve locally, mostly migrate to enum.nameDescription
Part of our codebase dates from the Boost.Python era and is being migrated to
pybind11. Boost.Python enums expose
namesandvaluesdicts, and existingPython code relies on lookups such as
MyEnum.values[i]andMyEnum.names["Foo"]. Today every one of those call sites has to be rewrittento
MyEnum.__members__/MyEnum(i)when the bindings move to pybind11.This PR adds two read-only static properties to every
py::enum_, matching theBoost.Python attributes:
names:dictmapping each member's name to the enum member(
{"Red": Color.Red, ...})values:dictmapping each member's integer value to the enum member(
{0: Color.Red, ...})Both are built from the existing
__entriesdict inenum_base::init, the sameway
__members__is, so there is no new state and no change tointernalsorthe ABI.
Testing
New
test_enum_names_valuesintests/test_enum.pycovers scoped and unscopedenums: contents of both dicts, identity of the returned members,
intkeys forvalues, access via an instance, read-only behavior, and that the returneddicts are copies.
test_enum.pypasses locally (CPython 3.13, GCC, Debug,PYBIND11_WERROR=ON).Limitations
An enum with a member literally named
namesorvalueswould collide withthe new properties. This is not covered here; happy to add handling or a test
for whichever behavior maintainers prefer.
AI assistance
Per
AI_POLICY.md: Claude Code (claude-opus-5-5) wrote the tests and draftedthis description. I wrote the feature commit, reviewed the generated tests,
and ran them locally. The test commit carries an
Assisted-bytrailer.Suggested changelog entry:
namesandvaluesstatic properties topy::enum_forBoost.Python compatibility.
🤖 Generated with Claude Code