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

Support for generic dataclasses #366

Closed
bilelomrani1 opened this issue Sep 1, 2023 · 2 comments · Fixed by #378
Closed

Support for generic dataclasses #366

bilelomrani1 opened this issue Sep 1, 2023 · 2 comments · Fixed by #378
Labels
enhancement New feature or request

Comments

@bilelomrani1
Copy link

🚀 Feature request

Add support for generic dataclasses in ArgumentParser.

Motivation

I often use generic dataclasses as configuration objects in libraries code. When building a CLI application, I would like to reuse these configurations to build a specialized parser.
Currently if a generic dataclass is used as a subparser, the argument parsing complains that all parameters are not typed and crashes.

from dataclasses import dataclass
from typing import Generic, TypeVar

from jsonargparse import ActionConfigFile, ArgumentParser

T = TypeVar("T")

# This is generic library code
@dataclass
class SubConfig(Generic[T]):
    generic: T
    a: int = 1
    b: float = 2.0

# This is CLI-specific
@dataclass
class Config:
    subconfig: SubConfig[int]


if __name__ == "__main__":
    parser = ArgumentParser(print_config="--print-config")
    parser.add_argument("--load", action=ActionConfigFile, help="Path to a YAML configuration")
    parser.add_dataclass_arguments(Config, "config")
    parser.parse_args()
$ python mre_jsonargparse.py --print-config
Traceback (most recent call last):
  File "/Users/bilelomrani/Documents/ILLUIN.nosync/instructions-finetuning/mre_jsonargparse.py", line 24, in <module>
    parser.add_dataclass_arguments(Config, "config")
  File "/Users/bilelomrani/Documents/ILLUIN.nosync/instructions-finetuning/.venv/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 435, in add_dataclass_arguments
    self._add_signature_parameter(
  File "/Users/bilelomrani/Documents/ILLUIN.nosync/instructions-finetuning/.venv/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 381, in _add_signature_parameter
    raise ValueError(
ValueError: With fail_untyped=True, all mandatory parameters must have a supported type. Parameter 'subconfig' from '__main__.Config.__init__' does not specify a type.

With fail_untyped=False, the printed config is empty.

Pitch

Would be cool if parser.add_dataclass_arguments would work with generic dataclasses and would generate a specialized subparser (in the case of my example, adding an int validator to config.subconfig.generic).

@bilelomrani1 bilelomrani1 added the enhancement New feature or request label Sep 1, 2023
@mauvilsa
Copy link
Member

mauvilsa commented Sep 1, 2023

Support for generic types would be a good addition to jsonargparse.

@mauvilsa
Copy link
Member

I have implemented support for generic types in #378. @bilelomrani1 please test it.

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

Successfully merging a pull request may close this issue.

2 participants