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

Type hints like list[str] for required arguments cause error #154

Closed
cod3licious opened this issue Jul 29, 2022 · 1 comment
Closed

Type hints like list[str] for required arguments cause error #154

cod3licious opened this issue Jul 29, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@cod3licious
Copy link

🐛 Bug report

Type hints for lists for required arguments break --help functionality (and probably more).

To reproduce

Script test.py:

from jsonargparse import CLI

def main(required_arg: list[str]):
    """
    Args:
        required_arg: a str list
    """
    pass

if __name__ == '__main__':
    CLI()

-> Running: python test.py --help results in

Traceback (most recent call last):
  File "/Users/franzi/Desktop/test.py", line 13, in <module>
    CLI()
  File "/Users/franzi/.local/share/virtualenvs/prism_minerva-QkUURSB3/lib/python3.10/site-packages/jsonargparse/cli.py", line 66, in CLI
    _add_component_to_parser(component, parser, as_positional, config_help)
  File "/Users/franzi/.local/share/virtualenvs/prism_minerva-QkUURSB3/lib/python3.10/site-packages/jsonargparse/cli.py", line 115, in _add_component_to_parser
    parser.add_function_arguments(component, **kwargs)
  File "/Users/franzi/.local/share/virtualenvs/prism_minerva-QkUURSB3/lib/python3.10/site-packages/jsonargparse/signatures.py", line 175, in add_function_arguments
    return self._add_signature_arguments(
  File "/Users/franzi/.local/share/virtualenvs/prism_minerva-QkUURSB3/lib/python3.10/site-packages/jsonargparse/signatures.py", line 229, in _add_signature_arguments
    self._add_signature_parameter(
  File "/Users/franzi/.local/share/virtualenvs/prism_minerva-QkUURSB3/lib/python3.10/site-packages/jsonargparse/signatures.py", line 329, in _add_signature_parameter
    action = group.add_argument(*args, **kwargs)
  File "/Users/franzi/.local/share/virtualenvs/prism_minerva-QkUURSB3/lib/python3.10/site-packages/jsonargparse/core.py", line 107, in add_argument
    action = super().add_argument(*args, **kwargs)
  File "/Users/franzi/.pyenv/versions/3.10.2/lib/python3.10/argparse.py", line 1408, in add_argument
    kwargs = self._get_optional_kwargs(*args, **kwargs)
  File "/Users/franzi/.pyenv/versions/3.10.2/lib/python3.10/argparse.py", line 1543, in _get_optional_kwargs
    raise ValueError(msg % args)
ValueError: invalid option string 'required_arg': must start with a character '-'

While running the same code instead with def main(required_arg: list): or def main(required_arg: list[str] = None): results in the expected help output.

Expected behavior

Running the code with def main(required_arg: list[str]): should also print out the help message and not cause an error.

Environment

  • jsonargparse version: 4.12.0
  • Python version: 3.10
  • How jsonargparse was installed: through pipenv with jsonargparse = {extras = ["argcomplete", "signatures"], version = ">=4.12.0"}
  • OS: macOS 12.4
@cod3licious cod3licious added the bug Something isn't working label Jul 29, 2022
@mauvilsa
Copy link
Member

mauvilsa commented Aug 1, 2022

Thank you reporting! I have just pushed a fix for it (commit aafbe17).

@mauvilsa mauvilsa closed this as completed Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants