omni-us / jsonargparse

Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables

Home Page:https://jsonargparse.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`CLI` fails to instantiate a Pydantic object if it has `typing.Optional` annotation

rusmux opened this issue Β· comments

πŸ› Bug report

CLI fails to instantiate a Pydantic object if it has typing.Optional annotation.

To reproduce

In main.py:

import typing as tp

import pydantic
from jsonargparse import CLI


class A(pydantic.BaseModel):
    value: int


class B:

    def __init__(self, a: tp.Optional[A] = None) -> None:
        self.a = a


def main(b: B) -> None:
    print(b)


if __name__ == "__main__":
    CLI(main)

In config.yaml:

b:
  class_path: __main__.B
  init_args:
    a:
      value: 1

In terminal:

python -m main --config config.yaml

Gives error:

error: Validation failed: Parser key "b":
  Problem with given class_path '__main__.B':
    Parser key "a":
      Does not validate against any of the Union subtypes
      Subtypes: (<class '__main__.A'>, <class 'NoneType'>)
      Errors:
        - __pydantic_private__
        - Expected a <class 'NoneType'>
      Given value type: <class 'jsonargparse._namespace.Namespace'>
      Given value: Namespace(value=1)

If I use this signature:

def __init__(self, a: A) -> None:
        self.a = a

Everything works as expected.

Expected behavior

The above code should run without errors.

Environment

  • jsonargparse version (e.g., 4.8.0): 4.29.0
  • Python version (e.g., 3.9): 3.9.19
  • How jsonargparse was installed (e.g. pip install jsonargparse[all]): pip install jsonargparse[all]
  • OS (e.g., Linux): macOS

Thank you for reporting! I will look at it.