-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: enable conversions between native Python enum types and C++ enums #5555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
80d9936
Apply smart_holder-branch-based PR #5280 on top of master.
rwgk 06eddc7
Add pytest.skip("GraalPy does not raise UnicodeDecodeError")
rwgk ce5859e
Add `parent_scope` as first argument to `py::native_enum` ctor.
rwgk 943d066
Replace `operator+=` API with `.finalize()` API. The error messages s…
rwgk 7689e5b
Resolve clang-tidy performance-unnecessary-value-param errors
rwgk 1a42257
Rename (effectively) native_enum_add_to_parent() -> finalize()
rwgk bab3348
Update error message: pybind11::native_enum<...>("Fake", ...): MISSIN…
rwgk c22104f
Pass py::module_ by reference to resolve clang-tidy errors (this is e…
rwgk 0da489d
test_native_enum_correct_use_failure -> test_native_enum_missing_fina…
rwgk fb10d3b
Add test_native_enum_double_finalize(), test_native_enum_value_after_…
rwgk 7acb18a
Clean up public/protected API.
rwgk 8b07a74
[ci skip] Update the Enumerations section in classes.rst
rwgk 4be8393
Merge branch 'master' into native_enum_master
rwgk 11f597b
Rename `py::native_enum_kind` → `py::enum_kind` as suggested by gh-he…
rwgk 190f99c
Experiment: StrEnum
rwgk df39090
Remove StrEnum code.
rwgk 6180ebc
Make enum_kind::Enum the default kind.
rwgk a0218f4
Catch redundant .export_values() calls.
rwgk 32e848a
[ci skip] Add back original documentation for `py::enum_` under new a…
rwgk ff25f5a
[ci skip] Add documentation for `py::enum_kind` and `py::detail::type…
rwgk 1c13866
Rename `Type` to `EnumType` for readability.
rwgk 8efb9bf
Eliminate py::enum_kind, use "enum.Enum", "enum.IntEnum" directly. Th…
rwgk 9918c09
Merge branch 'master' into native_enum_master
rwgk 9483e8c
EXPERIMENTAL StrEnum code. To be removed.
rwgk 0e46fa0
Remove experimental StrEnum code:
rwgk 346b47f
Add test with enum.IntFlag (no production code changes required).
rwgk 050febc
First import_or_getattr() implementation (dedicated tests are still m…
rwgk f773626
Fix import_or_getattr() implementation, add tests, fix clang-tidy err…
rwgk 29f143a
[ci skip] Update classes.rst: replace `py::enum_kind` with `native_ty…
rwgk f885205
Merge branch 'master' into native_enum_master
rwgk cbb7494
For "constructor similar to that of enum.Enum" point to https://docs.…
rwgk ae07bd2
Advertise Enum, IntEnum, Flag, IntFlags are compatible stdlib enum ty…
rwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _custom_type_caster: | ||
|
||
Custom type casters | ||
=================== | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
Deprecated | ||
########## | ||
|
||
.. _deprecated_enum: | ||
|
||
``py::enum_`` | ||
============= | ||
|
||
This is the original documentation for ``py::enum_``, which is deprecated | ||
because it is not `PEP 435 compatible <https://peps.python.org/pep-0435/>`_ | ||
(see also `#2332 <https://github.com/pybind/pybind11/issues/2332>`_). | ||
Please prefer ``py::native_enum`` (added with pybind11v3) when writing | ||
new bindings. See :ref:`native_enum` for more information. | ||
|
||
Let's suppose that we have an example class that contains internal types | ||
like enumerations, e.g.: | ||
|
||
.. code-block:: cpp | ||
|
||
struct Pet { | ||
enum Kind { | ||
Dog = 0, | ||
Cat | ||
}; | ||
|
||
struct Attributes { | ||
float age = 0; | ||
}; | ||
|
||
Pet(const std::string &name, Kind type) : name(name), type(type) { } | ||
|
||
std::string name; | ||
Kind type; | ||
Attributes attr; | ||
}; | ||
|
||
The binding code for this example looks as follows: | ||
|
||
.. code-block:: cpp | ||
|
||
py::class_<Pet> pet(m, "Pet"); | ||
|
||
pet.def(py::init<const std::string &, Pet::Kind>()) | ||
.def_readwrite("name", &Pet::name) | ||
.def_readwrite("type", &Pet::type) | ||
.def_readwrite("attr", &Pet::attr); | ||
|
||
py::enum_<Pet::Kind>(pet, "Kind") | ||
.value("Dog", Pet::Kind::Dog) | ||
.value("Cat", Pet::Kind::Cat) | ||
.export_values(); | ||
|
||
py::class_<Pet::Attributes>(pet, "Attributes") | ||
.def(py::init<>()) | ||
.def_readwrite("age", &Pet::Attributes::age); | ||
|
||
|
||
To ensure that the nested types ``Kind`` and ``Attributes`` are created within the scope of ``Pet``, the | ||
``pet`` ``py::class_`` instance must be supplied to the :class:`enum_` and ``py::class_`` | ||
constructor. The :func:`enum_::export_values` function exports the enum entries | ||
into the parent scope, which should be skipped for newer C++11-style strongly | ||
typed enums. | ||
|
||
.. code-block:: pycon | ||
|
||
>>> p = Pet("Lucy", Pet.Cat) | ||
>>> p.type | ||
Kind.Cat | ||
>>> int(p.type) | ||
1L | ||
|
||
The entries defined by the enumeration type are exposed in the ``__members__`` property: | ||
|
||
.. code-block:: pycon | ||
|
||
>>> Pet.Kind.__members__ | ||
{'Dog': Kind.Dog, 'Cat': Kind.Cat} | ||
|
||
The ``name`` property returns the name of the enum value as a unicode string. | ||
|
||
.. note:: | ||
|
||
It is also possible to use ``str(enum)``, however these accomplish different | ||
goals. The following shows how these two approaches differ. | ||
|
||
.. code-block:: pycon | ||
|
||
>>> p = Pet("Lucy", Pet.Cat) | ||
>>> pet_type = p.type | ||
>>> pet_type | ||
Pet.Cat | ||
>>> str(pet_type) | ||
'Pet.Cat' | ||
>>> pet_type.name | ||
'Cat' | ||
|
||
.. note:: | ||
|
||
When the special tag ``py::arithmetic()`` is specified to the ``enum_`` | ||
constructor, pybind11 creates an enumeration that also supports rudimentary | ||
arithmetic and bit-level operations like comparisons, and, or, xor, negation, | ||
etc. | ||
|
||
.. code-block:: cpp | ||
|
||
py::enum_<Pet::Kind>(pet, "Kind", py::arithmetic()) | ||
... | ||
|
||
By default, these are omitted to conserve space. | ||
|
||
.. warning:: | ||
|
||
Contrary to Python customs, enum values from the wrappers should not be compared using ``is``, but with ``==`` (see `#1177 <https://github.com/pybind/pybind11/issues/1177>`_ for background). |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. Is this warning captured anywhere else? It doesn't appear to be, but if we're keeping the old enum stuff it probably should be somewhere? Maybe this (and other things that were removed) could be added in a separate note?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
py::enum_
documentation is now here:https://pybind11--5555.org.readthedocs.build/en/5555/advanced/deprecated.html#deprecated-enum