Skip to content

pydantic subclass support #778

@findmyway

Description

@findmyway

To reproduce

from pydantic import BaseModel

from jsonargparse import auto_cli


class ModelBase(BaseModel): ...


class Model(ModelBase):
    name: str = "default"


class Experiment(ModelBase):
    model: ModelBase

exp = Experiment(model=Model())

def run(x:Experiment = exp):
    print(x)

if __name__ == "__main__":
    auto_cli(run)
  ...
  File ".venv/lib/python3.12/site-packages/jsonargparse/_core.py", line 208, in set_defaults
    raise NSKeyError(f'No action for key "{dest}" to set its default.')
jsonargparse._namespace.NSKeyError: No action for key "x.model" to set its default.

I'm not very sure whether it relates to previous discussions on dataclass and subclasses. All I can see after debugging is that an ArgumentGroup is created for x.model but there's not actions added to the _actions field.

Environment

  • jsonargparse version: 4.41.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions