Skip to content

Please support Literal type annotation and string defaults in str Enum annotations #1213

@Saya47

Description

@Saya47

Hello I like to use structured configs to have auto completion from the IDE but I have two issues:

First, Literal type annotation is not supported:

from dataclasses import dataclass
from typing import Literal
from omegaconf import OmegaConf


@dataclass
class MyConfig:
    mode: Literal["train", "test"] = "train"


conf = OmegaConf.structured(MyConfig)

Error:

ValidationError: Unexpected type annotation: Literal[train, test]
    full_key: mode
    object_type=MyConfig

Second issue is that if I use String Enum's, I cannot provide a string default for it anymore which is pretty annoying!

from dataclasses import dataclass
from typing import Literal
from omegaconf import OmegaConf
from enum import Enum


class Mode(str, Enum):
    Train = "train"
    Test = "test"


@dataclass
class MyConfig:
    mode: Mode = "train"


conf = OmegaConf.structured(MyConfig)

Error:

ValidationError: Invalid value 'train', expected one of [Train, Test]
    full_key: mode
    object_type=MyConfig

To alleviate this I would have to do mode: Mode = Mode("train")which is not very intuitive, so if I had Literal support I wouldn't even need to try Enums.

Thanks very much for this great library. It has helped me immensely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions