Skip to content

There can be only one (enum instance) #1177

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

Open
pschella opened this issue Nov 9, 2017 · 6 comments
Open

There can be only one (enum instance) #1177

pschella opened this issue Nov 9, 2017 · 6 comments

Comments

@pschella
Copy link
Contributor

pschella commented Nov 9, 2017

Currently enums compare equal with == but not with is.
I think it would be good if enums would be singletons like None instead.

@televator
Copy link

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

@lucmans
Copy link
Contributor

lucmans commented Jul 4, 2023

@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
Copy link
Collaborator

rwgk commented Jul 4, 2023

@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_enum as 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 merge will 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.

@lucmans
Copy link
Contributor

lucmans commented Jul 5, 2023

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.

@rwgk
Copy link
Collaborator

rwgk commented Jul 5, 2023

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.

@lucmans
Copy link
Contributor

lucmans commented Jul 7, 2023

Here is the PR.
#4732

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants