-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow the use of typing.Literal as a pythonic alternative to Enums #422
Comments
This functionality is covered well by enums. |
Sorry, linked to wrong issue. |
Upvoting the issue - the main use case is configuration serialization. When using Literal, pickling works without a hitch. When using Enum, we must have the appropriate class available in the scope where we unpickle the config. There are workarounds, but the easiest one is to just assert in runtime instead of work with an Enum... which defeats the purpose imo. |
how do use Enum instead of Litreral? |
Edit: oops I thought I was posting on the hydra repo, I think my question is still valid for OmegaConf but if not I can move this over to a hydra issue Does this also work if you're using the dataclass to define a schema (as described here)? It doesn't seem to actually do any type checking for me; i.e. given an app script: # app.py
import hydra
from dataclasses import dataclass
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf, MISSING
from enum import Enum
OPTIONS = ["model_a", "model_b", "model_c"]
OPTIONS_ENUM = Enum("OPTIONS", {k: k for k in OPTIONS})
@dataclass
class Config:
lr: float = MISSING
model: OPTIONS_ENUM = MISSING
cs = ConfigStore.instance()
cs.store(name="Config", node=Config)
@hydra.main(config_path="./", config_name="config", version_base='1.3')
def my_main(cfg: Config) -> None:
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
my_main() and a config file: # config.yaml
lr: 0.1
model: should_throw_exception this will happily run: $ python app.py
lr: 0.1
model: should_throw_exception |
There is nothing linking your config file with your config schema. |
Looks like there's a PR for this #865 that's gone stale. I'd love to add a vote for this to get reviewed and merged as it would be useful for me as well. |
|
Is your feature request related to a problem? Please describe.
Enums are a bit awkward to define in Python. On the other hand, using strings to represent a set of choices is very common in Python code.
Describe the solution you'd like
Describe alternatives you've considered
Enums, see above.
Additional context
My main motivation is to use this with Hydra.
The text was updated successfully, but these errors were encountered: