Skip to content

CLI does not work with callable component? #238

@indigoviolet

Description

@indigoviolet

🐛 Bug report

CLI currently breaks if I pass in a callable instance instead of a function. It would be nice not to break this abstraction.

To reproduce

from dataclasses import dataclass

from jsonargparse import CLI


@dataclass
class Foo:
    def __call__(self, x: int):
        return x


if __name__ == "__main__":
    f = Foo()
    CLI(f, as_positional=False)
❯ python test.py                                
Traceback (most recent call last):
  File "/home/venky/dev/instant-science/name_generator/test.py", line 14, in <module>
    CLI(f, as_positional=False)
  File "/home/venky/dev/instant-science/name_generator/.venv/lib/python3.9/site-packages/jsonargparse/cli.py", line 69, in CLI
    _add_component_to_parser(component, parser, as_positional, fail_untyped, config_help)
  File "/home/venky/dev/instant-science/name_generator/.venv/lib/python3.9/site-packages/jsonargparse/cli.py", line 116, in _add_component_to_parser
    added_args = parser.add_class_arguments(component, **kwargs)
  File "/home/venky/dev/instant-science/name_generator/.venv/lib/python3.9/site-packages/jsonargparse/signatures.py", line 70, in add_class_arguments
    raise ValueError(f'Expected "theclass" parameter to be a class type, got: {theclass}.')
ValueError: Expected "theclass" parameter to be a class type, got: Foo().

Expected behavior

It should work similar to def foo(x: int).

Environment

  • jsonargparse version (e.g., 4.8.0): 4.19
  • Python version (e.g., 3.9): 3.9
  • How jsonargparse was installed (e.g. pip install jsonargparse[all]): pip install jsonargparse[all]
  • OS (e.g., Linux): linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions