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 parse a list of unions of two different dataclasses

mnidza opened this issue · comments

🐛 Bug report

I am getting an error when parsing a list of objects whose type is the union of two different dataclasses.

To reproduce

In script.py

from dataclasses import dataclass
from jsonargparse import CLI

@dataclass
class A:
    a: int

@dataclass
class B:
    b: int

def f(c: list[A | B]):
    print(c)

CLI(f, as_positional=False)

In config.yaml

c:
  - a: 1
  - b: 2

In terminal

python script.py --config config.yaml

Expected behavior

Expected console output

[A(a=1), B(b=2)]

Actual console output

ValueError: Does not validate against any of the Union subtypes
Subtypes: (<class '__main__.A'>, <class '__main__.B'>)
Errors:
  - 'LazyInstance_B' object has no attribute 'b'
  - B.__init__() got an unexpected keyword argument 'a'
Given value type: <class 'jsonargparse._namespace.Namespace'>
Given value: Namespace(a=1)

Environment

  • jsonargparse version: 4.29.0
  • Python version: 3.11.4
  • How jsonargparse was installed: pip install jsonargparse[signatures]
  • OS: Windows 11 Enterprise, 10.0.22631

Thank you for reporting! I will look at it.