lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
384 stars 46 forks source link

Bug: unexpected keyword argument not detected when nesting dataclasses #295

Closed RmZeta2718 closed 5 months ago

RmZeta2718 commented 7 months ago

Describe the bug Normally, parser should raise an error when unexpected keyword argument occurs. But when dataclasses are nested, it doesn't raise an error but simply omits the unexpected keyword argument.

To Reproduce

test.py:

from dataclasses import dataclass
import simple_parsing

@dataclass
class ConfigNested:
    a: str = "hello"

@dataclass
class Config:
    config_nested: ConfigNested

args = simple_parsing.parse(Config, add_config_path_arg=True)
print(args)

config.yaml:

config_nested:
  a: world
  b: 123

Expected behavior

$ python test.py --config_path config.yaml
Traceback (most recent call last):

......

TypeError: ... got an unexpected keyword argument 'b'

Actual behavior Omits unexpected keyword argument 'b' and exit successfully:

$ python test.py --config_path config.yaml
Config(config_nested=ConfigNested(a='world'))

Desktop (please complete the following information):

stas00 commented 6 months ago

@lebrice, I was just about to make the same feature request.

If one makes a typo in a nested key - it just gets ignored and the default is used.

could simple-parsing plug https://github.com/pydantic/pydantic or something similar and provide a strict validation for all keys and values and not only the top-level ones?

This is currently a very big problem since if one makes a typo a whole training could be derailed and not noticed.

Thank you

lebrice commented 6 months ago

Thanks for posting!

Hmm this is not intended to happen, good catch!

lebrice commented 5 months ago

Hey there @RmZeta2718 @stas00 , sorry for the delay.

I believe this was fixed in #290. On the latest release (0.1.5) a RuntimeError is raised when there is an unexpected kwarg in the config.

Could you please check if the latest release also fixes that for you? If not, could you perhaps share an example that isn't working (where no exception is raised?)

Thanks a lot!