Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot parse a list of unions of two different dataclasses #525

Open
mnidza opened this issue Jun 12, 2024 · 3 comments
Open

Cannot parse a list of unions of two different dataclasses #525

mnidza opened this issue Jun 12, 2024 · 3 comments
Labels
bug Something isn't working pending author response

Comments

@mnidza
Copy link

mnidza commented Jun 12, 2024

🐛 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 mnidza added the bug Something isn't working label Jun 12, 2024
@mauvilsa
Copy link
Member

Thank you for reporting! I will look at it.

@nkrishnaswami
Copy link
Contributor

I think I'm encountering this or a similar bug when I try to parse a union of attrs classes. The attached testcase
fails when I try to use the second type in a union, with the following output:

E argparse.ArgumentError: Validation failed: Parser key "data":
E Does not validate against any of the Union subtypes
E Subtypes: (<class 'jsonargparse_tests.test_dataclass_like.AttrsData'>, <class 'jsonargparse_tests.test_dataclass_like.AttrsData2'>)
E Errors:
E - No action for key "p4" to set its default.
E - No action for key "p1" to set its default.
E Given value type: <class 'jsonargparse._namespace.Namespace'>
E Given value: Namespace(p1=None, p2='-', p4=1)

jsonargparse/_core.py:1010: ArgumentError

@mauvilsa
Copy link
Member

mauvilsa commented Sep 20, 2024

@mnidza just now I reran the reproduction script and it works as expected, giving [A(a=1), B(b=2)]. I am not sure in which change this was fixed. @nkrishnaswami also the TestAttrs::test_union test passes now. Please check and if you like, create a pull request to add your test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending author response
Projects
None yet
Development

No branches or pull requests

3 participants