Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace gets flattened if under a nested key #653

Open
carmocca opened this issue Dec 24, 2024 · 0 comments
Open

Namespace gets flattened if under a nested key #653

carmocca opened this issue Dec 24, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@carmocca
Copy link
Contributor

🐛 Bug report

Traceback (most recent call last):
  File "/home/ubuntu/carlos/kk.py", line 18, in <module>
    args = parser.instantiate_classes(args)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_deprecated.py", line 140, in patched_instantiate_classes
    cfg = self._unpatched_instantiate_classes(cfg, **kwargs)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_core.py", line 1207, in instantiate_classes
    component.instantiate_class(component, cfg)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_signatures.py", line 560, in group_instantiate_class
    parent[key] = instantiator_fn(group.group_class, **value)
  File "/home/ubuntu/.pyenv/versions/env/lib/python3.10/site-packages/jsonargparse/_common.py", line 155, in default_class_instantiator
    return class_type(*args, **kwargs)
TypeError: MyConfig.__init__() got an unexpected keyword argument 'optimizer.class_path'

To reproduce

from dataclasses import dataclass
from jsonargparse import ArgumentParser, Namespace
import torch

@dataclass
class Foo:
    f: int

@dataclass
class MyConfig:
    f: Foo
    optimizer: Namespace

parser = ArgumentParser()
parser.add_class_arguments(MyConfig, nested_key="my_config", skip={"optimizer"})
parser.add_subclass_arguments((torch.optim.Optimizer,), nested_key="my_config.optimizer", skip={"params"}, instantiate=False)
args = parser.parse_args(args=["--my_config.f.f=123", "--my_config.optimizer=torch.optim.Adam"])
args = parser.instantiate_classes(args)
print(args)

This works though (there is no nested key):

from dataclasses import dataclass
from jsonargparse import ArgumentParser, Namespace
import torch

@dataclass
class Foo:
    f: int

@dataclass
class MyConfig:
    f: Foo
    optimizer: Namespace

parser = ArgumentParser()
parser.add_class_arguments(MyConfig, skip={"optimizer"})
parser.add_subclass_arguments((torch.optim.Optimizer,), nested_key="optimizer", skip={"params"}, instantiate=False)
args = parser.parse_args(args=["--f.f=123", "--optimizer=torch.optim.Adam"])
args = parser.instantiate_classes(args)
print(args)

Expected behavior

Prints

Namespace(my_config=Namespace(f=Foo(f=123), optimizer=Namespace(class_path='torch.optim.Adam', init_args=Namespace(lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.0, amsgrad=False, foreach=None, maximize=False, capturable=False, differentiable=False, fused=None))))

Environment

  • jsonargparse version: 4.35.0
  • Python version: 3.10.12
  • How jsonargparse was installed: pip
  • OS: Linux

Merry christmas

@carmocca carmocca added the bug Something isn't working label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant