Open codeinred opened 4 months ago
Based on this test code it seems like implicit conversion to int is intended behavior.
def test_enum_to_int():
m.test_enum_to_int(m.Flags.Read)
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
m.test_enum_to_int(m.ScopedCharEnum.Positive)
m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
# ...
This is fine, but
float
is NOT intended behavior,Hey, I just wanted to check if theres an update on this in regards to disabling the implicit conversion from enum to int since I am running into the same problem where I want two separate constructors:
template <typename T>
constructLayer(std::unordered_map<int, py::array_t<T>>& channel_mapping);
template <typename T>
constructLayer(std::unordered_map<Enum::ChannelID, py::array_t<T>>& channel_mapping);
but it appears to always call the first ctor even when calling it with these args from python
# Passing these to the ctor always calls the int overload
image_dict = {}
image_dict[0] = np.full((height, width), 255, np.uint8)
image_dict[1] = np.full((height, width), 0, np.uint8)
image_dict[2] = np.full((height, width), 0, np.uint8)
image_dict_id = {}
image_dict_id[psapi.enum.ChannelID.red] = np.full((height, width), 255, np.uint8)
image_dict_id[psapi.enum.ChannelID.green] = np.full((height, width), 0, np.uint8)
image_dict_id[psapi.enum.ChannelID.blue] = np.full((height, width), 0, np.uint8)
Required prerequisites
What version (or hash if on master) of pybind11 are you using?
v2.11.1
Problem description
Enums (including
enum class
) will be implicitly converted to ints or floats during method calls. This behavior is surprising and can lead to problems where the wrong function is selected during overload resolution.There should be a way to disable implicit conversion of enums to other types.
Consider the following case:
Calling
f(0, Color.RED)
from python incorrectly calls thef(double, double)
overload. If theColor
enum were not implicitly convertible, this would not occur.Backwards Compatibility Concerns
I understand that there may be concerns about backwards compatibility. If these concerns are significant enough, then there should at least be a way to opt out of implicit conversion for enums
Reproducible example code
Full example code (C++):
Full example code (Python):
Output of python:
Is this a regression? Put the last known working version here if it is.
Not a regression