Skip to content

Literal types not validated when loading from YAML #321

Open

Description

@danny-schwartz-brt

Bug Description
When loading a config from a YAML file, Literal type annotation is not validated.

To Reproduce

from simple_parsing import ArgumentParser
from dataclasses import dataclass
from typing import Literal

@dataclass
class Foo:
   bar: Literal["one", 2] = "one"

if __name__ == "__main__":
   parser = ArgumentParser(
      config_path = "example.yml"
   )
   parser.add_arguments(Foo, "foo")
   args = parser.parse_args()
   foo: Foo = args.foo
   print(foo)

example.yml contents:

bar: "three"

Expected behavior
simple_parsing should raise an exception like "'one' or 'two' is required for field 'bar', 'three' is an invalid value"

Actual behavior
simple_parsing does not raise an exception

Activity

  1. lebrice commented on Aug 15, 2024

    @lebrice
    Owner

    Hey there @danny-schwartz-brt , thanks for raising the issue.

    This is interesting. I believe this is supposed to work, this might be a bug.

  2. danny-schwartz-brt commented on Aug 15, 2024

    @danny-schwartz-brt
    Author

    We're running into an issue where if we convert the dataclass object into a dict after reading it and then attempt to convert that dict into an object again with simple_parsing.helpers.serialization.serializable.from_dict(), we see the exception we expected to see earlier

  3. lebrice commented on Sep 6, 2024

    @lebrice
    Owner

    Hey @danny-schwartz-brt, have you tried using something like pydantic for the validation ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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