jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

Cannot parse a list of unions of two different dataclasses

Open mnidza opened this issue 8 months ago • 2 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

mnidza avatar Jun 12 '24 06:06 mnidza