Skip to content

feat: add enum names and values properties - #6314

Open
cielavenir wants to merge 3 commits into
pybind:masterfrom
cielavenir:addEnumNamesValues
Open

cielavenir wants to merge 3 commits into
pybind:masterfrom
cielavenir:addEnumNamesValues

Conversation

@cielavenir

@cielavenir cielavenir commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Background

I'm trying to build our software using stock pybind11.

our patch list:

Description

Part of our codebase dates from the Boost.Python era and is being migrated to
pybind11. Boost.Python enums expose names and values dicts, and existing
Python code relies on lookups such as MyEnum.values[i] and
MyEnum.names["Foo"]. Today every one of those call sites has to be rewritten
to MyEnum.__members__ / MyEnum(i) when the bindings move to pybind11.

This PR adds two read-only static properties to every py::enum_, matching the
Boost.Python attributes:

  • names: dict mapping each member's name to the enum member
    ({"Red": Color.Red, ...})
  • values: dict mapping each member's integer value to the enum member
    ({0: Color.Red, ...})

Both are built from the existing __entries dict in enum_base::init, the same
way __members__ is, so there is no new state and no change to internals or
the ABI.

Testing

New test_enum_names_values in tests/test_enum.py covers scoped and unscoped
enums: contents of both dicts, identity of the returned members, int keys for
values, access via an instance, read-only behavior, and that the returned
dicts are copies. test_enum.py passes locally (CPython 3.13, GCC, Debug,
PYBIND11_WERROR=ON).

Limitations

An enum with a member literally named names or values would collide with
the 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 drafted
this description. I wrote the feature commit, reviewed the generated tests,
and ran them locally. The test commit carries an Assisted-by trailer.

Suggested changelog entry:

  • Added names and values static properties to py::enum_ for
    Boost.Python compatibility.

🤖 Generated with Claude Code

@cielavenir

Copy link
Copy Markdown
Contributor Author

reviewed the pull request description

@cielavenir
cielavenir marked this pull request as ready for review November 26, 2026 02:15
@henryiii

Copy link
Copy Markdown
Collaborator

Quick question: can you use py::native_enum? We are moving away from the old non-stdlib compatible enum, I think.

@henryiii

Copy link
Copy Markdown
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 import enum in Python. You can add them yourself trivially, though, for backward compatibility: when you define your enum, just add these methods, right after .finalize(). No upstream change needed.

The standard way to access these in Python or pybind11 is C(i), C["Foo"] and C.__members__, which all work. C.values = {m.value: m for m in C} and C.names = dict(C.__members__) would do this in Python.

This branch has not been deployed

No deployments
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.

2 participants