omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.98k stars 113 forks source link

Support `Boost.Python.enum` Enums as annotation and supported type. #1160

Open Daraan opened 8 months ago

Daraan commented 8 months ago

Is your feature request related to a problem? Please describe. When an Enum is implemented in C++ and brought to Python via the Boost.Python.enum class is not supported like normal Enums despite being alike.

flags=dict(allow_objects=True) needs to be used to allow these Enum type hints, however there will then be no safeguarding and any value can be written into these fields. Further Union cannot used with them.

Describe the solution you'd like Boost.Python.enum should possible as type like the standard Python Enums


A workaround is to construct a intermediate enum. One would need to reconstruct all enums by hand or use the functional approach:

DummyEnum = Enum("DummyEnum", {str(name):value for value, name in RealEnum.values.items()})

@dataclass
class Mine():
    greeting: DummyEnum = RealEnum.HELLO # type: ignore

However the functional approach does not allow for correct type hints here.

odelalleau commented 8 months ago

This seems like a niche use case that is unlikely to ever get supported unless someone who really needs it submits a complete PR for it. Also unless Boost.Python.enum is a subclass of enum.Enum this may require a lot of changes in the code (and if it is I'm not 100% sure how easy it'd be...)