Skip to content

Help docstring for Callable does not work as intended #566

@MiguelMonteiro

Description

@MiguelMonteiro

When using a Callable argument that returns one class out of a union of classes, the help string does not show the docstring of the classes. This bug has been mentioned here before. This particular use case is as in the issue mentioned above, we want to select and parameterize a pytorch optimizer via the command line and display help strings for the choice the user makes.

To reproduce

from jsonargparse import ArgumentParser
from typing import Callable, Iterable
from torch.optim import SGD, Adam, AdamW

parser = ArgumentParser(exit_on_error=False)
# Here add to the parser only argument(s) relevant to the problem
parser.add_argument("--optimizer", type=Callable[[Iterable], SGD | Adam | AdamW])

# Preferable that the command line arguments are given to the parse_args call
result = parser.parse_args(["--optimizer.help=SGD"])

# If the problem is in the parsed result, print it to stdout
print(parser.dump(result))

Gives the following error:

argparse.ArgumentError: Parser key "optimizer": Type typing.Callable[[typing.Iterable], torch.optim.sgd.SGD | torch.optim.adam.Adam | torch.optim.adamw.AdamW] expects a function or a callable class: Dict must include a class_path and optionally init_args, but got NestedArg(key='help', val='SGD'). Got value: Namespace(help='SGD')

Expected behavior

To print the docstring and parameters descriptions provided by torch.optim.SGD or any other selected class.

Environment

  • jsonargparse version: 4.32.1
  • Python version: 3.10
  • How jsonargparse was installed: pip
  • OS: macOS 14.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions