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
302 stars 41 forks source link

Fields of optional dataclasses are not displayed in the generated help #509

Closed maticus closed 2 months ago

maticus commented 2 months ago

🐛 Bug report

Prompted by the super-quick fix of #507 (thanks!) I'm doing some more testing of jsonargparse with nested optional dataclasses, and found out:

Fields of optional dataclasses are not displayed in the generated help

To reproduce

Following script

import jsonargparse
from typing import Optional
from dataclasses import dataclass

@dataclass
class B:
    c: int = 3

@dataclass
class A:
    b: B
    ob: Optional[B]

def fun(a: A):
    print(a)

jsonargparse.CLI(fun)

when ran: python python-jsonargparse-optional-dataclass.py -h

displays:

...
  -h, --help            Show this help message and exit.
  --config CONFIG       Path to a configuration file.
  --print_config[=flags]
                        Print the configuration after applying all other arguments and exit. The optional flags customizes the output and
                        are one or more keywords separated by comma. The supported flags are: comments, skip_default, skip_null.

A(b: __main__.B, ob: Optional[__main__.B]):
  --a CONFIG            Path to a configuration file.
  --a.ob OB             (type: Optional[B], default: null)

B(c: int = 3):
  --a.b CONFIG          Path to a configuration file.
  --a.b.c C             (type: int, default: 3)

Note, --a.ob.c is missing, but I can configure using this option:

$ python python-jsonargparse-optional-dataclass.py  --a.b.c 77 --a.ob.c 78
A(b=B(c=77), ob=B(c=78))

Expected behavior

The --a.ob.c option should also be displayed in the help.

Environment

mauvilsa commented 2 months ago

Thank you for reporting! This is actually a feature request duplicate of #368. The proposal is to do as described in https://github.com/omni-us/jsonargparse/issues/368#issuecomment-1705438863, though this hasn't yet been implemented. I will close this issue in favor of the previous one.