-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Comments
It isn't intended to support Why do you want to use |
How else should I annotate the type of the optimizer arguments? I dont know beforehand the optimizer type and its arguments. They depend on what the user passes for Also, it's notable that this works well without a nested key. That's why I thought this was just an edge case Thanks for your help! |
Type |
It doesn't. The error is the same.
Is this what you suggest? from dataclasses import dataclass
from jsonargparse import ArgumentParser
import torch
from typing import Callable, Iterable
from torch.optim import Optimizer
@dataclass
class Foo:
f: int
@dataclass
class MyConfig:
f: Foo
optimizer: Callable[[Iterable], Optimizer]
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) I am mainly interested in the resulting namespace. Getting a function to instantiate is convenient but not blocking (I can get that myself). The error when the |
What I meant was @dataclass
class Foo:
f: int
@dataclass
class MyConfig:
f: Foo
optimizer: Callable[[Iterable], Optimizer]
parser.add_class_arguments(MyConfig, nested_key="my_config")
args = parser.parse_args(args=["--my_config.f.f=123", "--my_config.optimizer=torch.optim.Adam"])
print(args.my_config.optimizer) # This is what you want
init = parser.instantiate_classes(args)
print(init.my_config.optimizer) # This you don't want but can ignore |
I think this worked, but I hit another issue. The optimizer works but not my classes: #655 Closing this one. Thanks |
🐛 Bug report
To reproduce
This works though (there is no nested key):
Expected behavior
Prints
Environment
Merry christmas
The text was updated successfully, but these errors were encountered: