omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables

Home Page:https://jsonargparse.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

maticus opened this issue Β· comments

πŸ› 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

  • jsonargparse version: 4.28.0:
  • Python version: 3.10.12:
  • How jsonargparse was installed: pip install jsonargparse
  • OS: Ubuntu Jammy

Probably related to #423

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