Skip to content

Cannot instantiate a class from env variables #680

Description

@rusmux

🐛 Bug report

Cannot instantiate a class from env variables.

To reproduce

In main.py:

from jsonargparse import CLI


class Bar:

    def __init__(self, v: int) -> None:
        self._v = v


def main(b: Bar) -> None:
    print(b._v)


if __name__ == "__main__":
    CLI(main, default_env=True, env_prefix="FOO")

In shell:

export FOO_B__V=2
python -m main

Will result in the error:

usage: main.py [-h] [--config CONFIG] [--print_config[=flags]] [--b.help [CLASS_PATH_OR_NAME]] b
error: Validation failed: Key "b" is required but not included in config object or its value is None.

I can instantiate this class with:

export FOO_B="{v: 1}"
python -m main

But I cannot even override v:

export FOO_B__V=2
python -m main

Will still result in v being 1.

Expected behavior

I can instantiate a class from env variables. At least override fields.

Environment

  • jsonargparse version: 4.37.0
  • Python version: 3.12.8
  • How jsonargparse was installed: uv pip install -U "jsonargparse[all]"
  • OS: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions