lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
401 stars 50 forks source link

Fix parsing defaults for nested dataclasses #113

Closed iamgroot42 closed 2 years ago

iamgroot42 commented 2 years ago

When parsing a dataclass that has other dataclass fields in turn, and a default dataclass instance is provided, the parser in its current form ignores values for children dataclasses if they are default, even if the given configuration explicitly provides those values.

For instance,


@dataclass
class A:
  p: int
  q: float

@dataclass
class B:
  x: int
  y: Optional[A] = None

For this configuration, a dataclass corresponding to the following configuratiton:

{'B': 'x': 3, 'y': {p: 4, q: 0.1}}

when parsed by the module, will result in

{'B': x}
lebrice commented 2 years ago

Hey there, thanks a lot for the PR! @iamgroot42 I've added two tests for this, and also a fix for the Optional[A] case, you gave above. I will create a new PR that will include your original message and commit, with my additional commit(s) on top. Is that ok with you? Or would you rather I make a PR to this branch, and then after you accept it, we merge your PR to master?

iamgroot42 commented 2 years ago

@lebrice thanks! Either is fine by me :)