Replies: 5 comments 8 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
The things I've noticed so far are:
I think the Changing comparison semantics would be a breaking change, but also seems pretty important for type-safety! I would actually contend that the current behavior is a bug: since all enums with the same integral value have the same hash and also compare equal, you can't put two different enumerators (from different enums) in the same dict/set if they have the same value. Allowing enum objects whose values aren't in the enumerated list is a larger change and might be out-of-scope (and I think nanobind's current enumerators-are-singletons semantics are a better fit for most enums in that respect anyway). I think it would not be too hard to provide a It would also be nice to add pickle support. For most classes it's easy enough to write Any interest in supporting syntax for looking up an enumerator by its name? I am happy to implement any and all of these, just want to seek consensus on the design first. |
Beta Was this translation helpful? Give feedback.
-
I just noticed that enums aren't actually singletons if you return them from C++: (
Similarly, you can create an invalid enum value (by returning it from C++) that will only sort of work in Python:
It seems like it would be good to normalize the behavior here, so enum values returned from C++ work like enum values constructed from Python. I would propose resolving this via something like:
|
Beta Was this translation helpful? Give feedback.
-
@wjakob Do you have objections against adding support for Python-style enum methods to nanobind enums, including metaclass methods such as |
Beta Was this translation helpful? Give feedback.
-
I happened to come across this thread and was wondering if it's ever been considered to allow for typing C++ enums as Python IntFlag enums? (there are more variants of this, picking this one up as an example). This seems like a handy and useful support-type for using with bit flags. For example:
There's also an IntEnum for stuff that's not bit fields. This seems to have useful functionality out of the box, like being able to do arithmetic on the enums. But I'm not so experienced with Python bindings or nanobind that I could tell if using something like Python's enum IntFlag directly is possible or comes with a lot of downsides. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There isn't much documentation about the detailed semantics of
nb::enum_
s, and it's pretty different from pybind11 enums. I've made some changes to add missing functionality, which I'd like to upstream, but before I go that route, I'm wondering what the intended mental model for anb::enum_
is. Is the goal to mimic Pythonenum.Enum
? Mimic C++? Why is there a separatenb_enum
metaclass but it doesn't add anything to the type object? Is there some future expansion planned there?Beta Was this translation helpful? Give feedback.
All reactions