Skip to content
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

Literal types not validated when loading from YAML #321

Open
danny-schwartz-brt opened this issue Aug 15, 2024 · 3 comments
Open

Literal types not validated when loading from YAML #321

danny-schwartz-brt opened this issue Aug 15, 2024 · 3 comments

Comments

@danny-schwartz-brt
Copy link

danny-schwartz-brt commented Aug 15, 2024

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

@lebrice
Copy link
Owner

lebrice commented Aug 15, 2024

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.

@danny-schwartz-brt
Copy link
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

@lebrice
Copy link
Owner

lebrice commented Sep 6, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants