omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
https://jsonargparse.readthedocs.io
MIT License
326 stars 49 forks source link

Empty tuple default value not supported #589

Closed MiguelMonteiro closed 1 month ago

MiguelMonteiro commented 1 month ago

🐛 Bug report

Empty tuples are not supported by the parser.

To reproduce

from dataclasses import dataclass

from jsonargparse import ArgumentParser

@dataclass
class TestCfg:
    a: tuple[int, ...] = ()

if __name__ == "__main__":
    parser = ArgumentParser(description="CLI for training models", exit_on_error=False)
    parser.add_argument("test", type=TestCfg)
    args = parser.parse_args()
    print(args)

Running the following program without passing any arguments or by passing an empty list/tuple "[]" results in the following error:

argparse.ArgumentError: Validation failed: Parser key "test.a":
  Expected a non-empty tuple. Got value: []

Expected behavior

To be able to parse empty tuples without errors.

Environment

mauvilsa commented 1 month ago

Thank you for reporting!

This looks like an easy fix. Just adding a unit test and extending this if condition https://github.com/omni-us/jsonargparse/blob/a8b8b151ab26db57cae94c6f5de9c618daf4971b/jsonargparse/_typehints.py#L850

@MiguelMonteiro would you like to contribute de fix?

MiguelMonteiro commented 1 month ago

Hi @mauvilsa I am happy to contribute, I will open a PR when I get the time.