omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
319 stars 47 forks source link

Cannot use optional dataclasses defaulting to not-null in the configuration hierarchy #507

Closed maticus closed 4 months ago

maticus commented 4 months ago

🐛 Bug report

Cannot use optional dataclasses defaulting to not-null in the configuration hierarchy.

To reproduce

Run script:

import jsonargparse
from typing import Optional
from dataclasses import dataclass, field

@dataclass
class B:
    c: int = 3

@dataclass
class A:
    # THIS CAUSES AN ERROR
    b: Optional[B] = field(default_factory=B)
    # THIS IS FINE
    #b: B = field(default_factory=B)

def fun(a: A):
    print(a)

jsonargparse.CLI(fun)

We get

usage: python-jsonargparse-optional-dataclass.py [-h] [--config CONFIG] [--print_config[=flags]] [--a CONFIG] [--a.b B]
error: Validation failed: Parser key "a.b":
  Does not validate against any of the Union subtypes
  Subtypes: (<class '__main__.B'>, <class 'NoneType'>)
  Errors:
    - Type <class '__main__.B'> expects a dict or Namespace
    - Expected a <class 'NoneType'>
  Given value type: <class '__main__.B'>
  Given value: B(c=3)

Expected behavior

Script should write: A(b=B(c=3))

Environment

maticus commented 4 months ago

Probably related to #423

mauvilsa commented 4 months ago

Thank you for reporting! This is not related to #423.