Skip to content

Instantiation of custom class leads to wrong defaults #460

@awaelchli

Description

@awaelchli

🐛 Bug report

When instantiating objects from a custom class where a default is specified in the signature, jsonargparse infers the defaults from the the wrong class:

To reproduce

from jsonargparse import CLI


class Super:
    pass


class Default(Super):
    def __init__(self, foo: str = "foo", bar: str = "bar"):
        self.foo = foo
        self.bar = bar


class Other(Super):
    def __init__(self, foo: str = "foo", bar: str = "baaaaar"):
        self.foo = foo
        self.bar = bar


def main(data: Super = Default()):
    print(data.foo)
    print(data.bar)


if __name__ == "__main__":
    CLI(main)
python repro.py --data Other --data.foo test

Output:

test
bar

Expected behavior

I expect the output to be

test
baaaaar

because I selected --data Other, so it should use the defaults from the Other class.

Environment

  • jsonargparse version (e.g., 4.8.0): 4.27.5
  • Python version (e.g., 3.9): 3.10
  • How jsonargparse was installed (e.g. pip install jsonargparse[all]): pip install -U jsonargparse[signatures]
  • OS (e.g., Linux): MacOS and Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions