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

Enum inheritance Support #1061

Open lequytra opened 1 year ago

lequytra commented 1 year ago

Is your feature request related to a problem? Please describe. I have the following code structure:

class EnumBase(Enum):
    pass

class VanillaEnum(EnumBase):
    # some enum values

class ExtendedEnum(EnumBase):
    # some enum values

class Config:
    enum_options: EnumBase

I want Enum inheritance to be supported, such that the enum_options to accept both VanillaEnum and ExtendedEnum. Currently, OmegaConf is throwing errors in the validate_and_convert_to_enum method, that the enums are not valid since it doesn't belong to EnumBase.

Describe the solution you'd like I want Enum Inheritance to be supported so enum_options of type EnumBase can accept child enum classes. Can we: in the validate_and_convert_to_enum method, get the subclasses of enum_type using enum_type.__subclasses__() and try to return subclass[value] when enum_type[value] failed?

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

odelalleau commented 1 year ago

My 2 cents: