Skip to content

There can be only one (enum instance) #1177

Description

@pschella

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

Activity

  1. televator commented on Jun 4, 2019

    @televator

    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
    
  2. lucmans commented on Jul 4, 2023

    @lucmans
    Contributor

    @rwgk We're still running into this issue on pybind11 2.9.1 with the minimal repro provided by @televator. Any update on this?

  3. rwgk commented on Jul 4, 2023

    @rwgk
    Collaborator

    @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.

  4. lucmans commented on Jul 5, 2023

    @lucmans
    Contributor

    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.

  5. rwgk commented on Jul 5, 2023

    @rwgk
    Collaborator

    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.

  6. lucmans commented on Jul 7, 2023

    @lucmans
    Contributor

    Here is the PR.
    #4732

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions