omry / omegaconf

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

OmegaConf doesn't properly support StrEnum #1182

Open jeremyk opened 2 months ago

jeremyk commented 2 months ago

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

jeremyk commented 2 months ago

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.

omry commented 2 months ago

Feel free to suggest patch to fix it (with tests).