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
317 stars 44 forks source link

Unable to specify a path for a callable parameter of a class #419

Closed function2-llx closed 9 months ago

function2-llx commented 10 months ago

🐛 Bug report

When define a parameter of a class to be callable type, it is unable to specify a path for this parameter.

To reproduce

from collections.abc import Callable

from jsonargparse import ArgumentParser

class B:
    def __init__(self, x: int):
        self.x = x

class A:
    def __init__(self, b: Callable[[int], B]):
        self.b = b(id(self))

def main():
    parser = ArgumentParser()
    parser.add_class_arguments(A, 'a', sub_configs=True)
    '''
        content of b.yaml:
        class_path: B
    '''
    args = parser.parse_args(['--a.b', 'b.yaml'])

if __name__ == '__main__':
    main()

Expected behavior

Actual output

usage: test.py [-h] [--a CONFIG] --a.b B
error: Parser key "a.b":
  Type collections.abc.Callable[[int], __main__.B] expects a function or a callable class: No module named 'b'. Got value: b.yaml

Environment

mauvilsa commented 9 months ago

Fixed with #424. Thank you for reporting!