Repository navigation
There can be only one (enum instance) #1177
Description
Activity
This is still an issue with 2.2.4 and very error-prune as the python doc explicitly encourages to check enum values for identity.
Minimal example to reproduce
#include <pybind11/pybind11.h> namespace py = pybind11; PYBIND11_MODULE(example_py, m) { enum class ScopedEnum { Two = 2, Three }; m.def("return_two", [](){return ScopedEnum::Two;}); py::enum_<ScopedEnum>(m, "ScopedEnum") .value("Two", ScopedEnum::Two) .value("Three", ScopedEnum::Three); }produces this situation
In [1]: import example_py In [2]: x = example_py.ScopedEnum.Two In [3]: y = example_py.ScopedEnum.Two In [4]: z = example_py.return_two() In [5]: x is y # BUT... Out[5]: True In [6]: x is z Out[6]: False In [7]: x == z Out[7]: True@rwgk We're still running into this issue on pybind11 2.9.1 with the minimal repro provided by @televator. Any update on this?
@rwgk We're still running into this issue on pybind11 2.9.1 with the minimal repro provided by @televator. Any update on this?
I could be wrong, but I'm operating under the assumption that the
py::enum_behavior is practically unchangeable/unfixable, merely because it's in wide use.Therefore I added
py::native_enumas an alternative here:Note that that's in another repo, although it's actually more-or-less just another branch.
Could this be merged into pybind11 master? — In theory yes, easily (
git mergewill just work), in practice it seems extremely unlikely to me. pybind11 master has been in extremely conservative mode for a very long time.You could use the pywrapcc branch and it'll fully interoperate with pybind11 master. There is just no pip-installable release.
Thank you for the quick response. It's very unfortunate that it won't likely be merged into master.
I'll look into the native enum in your repo.Since the Python docs seem to prefer comparison by identity, I think the pybind11 docs should put a warning in the related section that it isn't supported.
I think the pybind11 docs should put a warning in the related section that it isn't supported.
Please send a PR. I'll look quickly.
Here is the PR.
#4732
Currently enums compare equal with
==but not withis.I think it would be good if enums would be singletons like
Noneinstead.