jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

Cannot configure fields of optional dataclasses using env-vars

Open maticus opened this issue 9 months ago • 1 comments

🐛 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:

Cannot configure fields of optional dataclasses using env-vars

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, default_env=True, env_prefix="FOO")

when ran: FOO_A__OB__C=7 FOO_A__B__C=8 python python-jsonargparse-optional-dataclass.py

displays: A(b=B(c=8), ob=None)

Note, I can still configure this filed using --a.ob.c:

$ 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

FOO_A__OB__C=7 env-var should configure the appropriate field.

Additionally, the FOO_A__OB__C should be visible in generated help (running script with -h) - related to #509

Environment

  • jsonargparse version: today's master :-) (git-describe: v4.28.0-10-gf44ba32)
  • Python version: 3.10
  • How jsonargparse was installed: pip install -e ".[dev,all]" from checked-out repo.
  • OS: Ubuntu Jammy

maticus avatar May 23 '24 07:05 maticus