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
302 stars 41 forks source link

Cannot parse a list of dataclasses with optional dataclass member #524

Closed mnidza closed 1 month ago

mnidza commented 1 month ago

🐛 Bug report

I am getting an when parsing a list of dataclass objects, where the dataclass has an optional member which is another dataclass.

To reproduce

In script.py

from dataclasses import dataclass
from typing import Optional
from jsonargparse import CLI

@dataclass
class A:
    x: int

@dataclass
class B:
    a: Optional[A] = None

def f(b: list[B]):
    print(b)

CLI(f, as_positional=False)

In config.yaml

b:
  - a:
      x: 1

In terminal

script.py --config config.yaml

Expected behavior

Expected console output

[B(a=A(x=1))]

Actual console output

usage: script.py [-h] [--config CONFIG] [--print_config[=flags]] --b B
error: Validation failed: No action for key "a.x" to set its default.

Environment

mauvilsa commented 1 month ago

Thank you for reporting! This is fixed with #527.