Skip to content

Better Union Parsing #684

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

Closed
trporn opened this issue Feb 26, 2025 · 2 comments
Closed

Better Union Parsing #684

trporn opened this issue Feb 26, 2025 · 2 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@trporn
Copy link

trporn commented Feb 26, 2025

🚀 Feature request

When parsing a parameter that is a Union of types, there are some deficiencies in the parsing. I am borrowing from the bug report at #588. By adding a field with the class name, I can successfully parse all Union types:

from dataclasses import dataclass
from jsonargparse import CLI

@dataclass
class A:
    A: bool 
    foo: int = 3

@dataclass
class B:
    B: bool
    foo: int = 3
    bar: float = 1.0

def func(params: A | B):
    print(params)

if __name__ == "__main__":
     CLI(func)

We can now choose the class to parse like so (would be even nicer with a store-false type):

python myscript.py --params.A=true

However, it is not possible to get help for the type chosen, which severly limits the user's ability to actually use it. It would make it more usable if writing:

python myscript.py --params.A=true --help

would print the help for type A.

Motivation

Enable parsing of Union types including a verbose help.
This is related to #588. While the hack above solves the parsing itself, it is extremely limited if the user cannot get the help he needs for the chosen type.

Pitch

If there is a way to get help for the chosen type by writing something like:

python myscript.py --params.A=true --help

This would be good enough to support union types flawlessly.

Alternatives

@trporn trporn added the enhancement New feature or request label Feb 26, 2025
@mauvilsa mauvilsa added the duplicate This issue or pull request already exists label Feb 27, 2025
@mauvilsa
Copy link
Member

Thank you for proposing improvements!

As mentioned in #588 (comment), which points to #287 (comment), the plan is to make dataclasses behave like subclasses. Actually this proposal seems to be a duplicate of #368, where I explain the help behavior, see #368 (comment). It would not be as you propose, but like python myscript.py --params.help A.

I have already spent a lot of time on an implementation for #287, but it still requires considerable work and take time. I will think about the possibility of adding the help part for dataclasses without the full subclass support. But I would only do that if it is not a significant additional effort.

Closing this as duplicate in favor of #368.

@trporn
Copy link
Author

trporn commented Feb 27, 2025

Thanks for this.
As I am new to jsonargsparse I missed the key point that everything I want to achieve is readily available if I use regular classes and subclasses instead of dataclasses. For me this compromise is acceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants