You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
fromdataclassesimportdataclassfromtypingimportGeneric, TypeVarfromjsonargparseimportActionConfigFile, ArgumentParserT=TypeVar("T")
# This is generic library code@dataclassclassSubConfig(Generic[T]):
generic: Ta: int=1b: float=2.0# This is CLI-specific@dataclassclassConfig:
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).
The text was updated successfully, but these errors were encountered:
🚀 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.
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 anint
validator toconfig.subconfig.generic
).The text was updated successfully, but these errors were encountered: