OmegaConf doesn't properly support StrEnum
Describe the bug In the example in the docs for Enums these all work:
>>> conf.height = Height.TALL
>>> assert conf.height == Height.TALL
>>> # The name of Height.TALL is TALL
>>> conf.height = "TALL"
>>> assert conf.height == Height.TALL
>>> # The ordinal of Height.TALL is 1
>>> conf.height = 1
>>> assert conf.height == Height.TALL
but for Str enums (same as docs but use StrEnum instead):
from enum import StrEnum
class Height(StrEnum):
SHORT = "short"
TALL = "tall"
The first two examples work as with regular enums but the third:
>>> conf.height = "tall"
gives: omegaconf.errors.ValidationError: Invalid value 'tall', expected one of [SHORT, TALL]
To Reproduce See description
Expected behavior You would be able to set a StrEnum with the key or the value.
Additional context
- [latest ] OmegaConf version: <FILL>
- [3.11+ ] Python version: <FILL>
- [any] Operating system: <FILL>
- [ x] Please provide a minimal repro
One more note - this is an issue for us as when we write out the config it will use the value ("tall" in this case) but that same config can't be read back in as it will throw the above error.
Feel free to suggest patch to fix it (with tests).
The fix is as simple as changing https://github.com/omry/omegaconf/blob/117f7de07285e4d1324b9229eaf873de15279457/omegaconf/nodes.py#L490
from
return enum_type[value] to return enum_type(value)
That change allows the enum to be constructed from strings through the missing operator as well, which resolves the issue.
I know this isn't a proper patch with some suitable tests, but I could verify that it works for my local StrEnum enum.