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]
🐛 Bug report
I am getting an error when parsing a list of objects whose type is the union of two different
dataclass
es.To reproduce
In
script.py
In
config.yaml
In terminal
Expected behavior
Expected console output
Actual console output
Environment
pip install jsonargparse[signatures]