-
Notifications
You must be signed in to change notification settings - Fork 24
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
Overriding with YAML defaults on a dataclass config #27
Comments
Hi! So the way This is hinted at in the signature of
Does that general pattern make sense? Another way to think about it is that the default value we expect should be valid on the right side of the equality here: def main(config: Config = some_default) -> None:
pass
tyro.cli(main) |
In terms of solutions, here are a couple I can think of. I personally prefer to avoid YAML files when possible, so don't feel super strongly about any particular one:
from typing import TypedDict
import yaml
import tyro
class Config(TypedDict):
exp_name: str
batch_size: int
# YAML configuration. Note that this could also be loaded from a file! Environment
# variables are an easy way to select between different YAML files.
default_yaml = r"""
exp_name: test
batch_size: 10
""".strip()
if __name__ == "__main__":
# Convert our YAML config into a nested dictionary.
default_config = dict(yaml.safe_load(default_yaml))
# Override fields in the dictionary.
overridden_config = tyro.cli(Config, default=default_config)
import yaml
import tyro
@dataclasses.dataclass
class Config:
exp_name: str
batch_size: int
# YAML configuration. Note that this could also be loaded from a file! Environment
# variables are an easy way to select between different YAML files.
default_yaml = r"""
exp_name: test
batch_size: 10
""".strip()
if __name__ == "__main__":
import dacite
# Convert our YAML config into a dataclass instance.
default_config = dacite.from_dict(Config, yaml.safe_load(default_yaml))
# Override fields in the dictionary.
overridden_config = tyro.cli(Config, default=default_config)
import dataclasses
import yaml
import tyro
@dataclasses.dataclass
class Config:
exp_name: str
batch_size: int
# YAML configuration. Note that this could also be loaded from a file! Environment
# variables are an easy way to select between different YAML files.
default_yaml = r"""
!dataclass:Config
exp_name: test
batch_size: 10
""".strip()
if __name__ == "__main__":
# Convert our YAML config into a dataclass instance.
default_config = tyro.extras.from_yaml(Config, default_yaml)
# Override fields in the dictionary.
overridden_config = tyro.cli(Config, default=default_config) |
This problem has puzzled me for a long time. Thanks for your clear explanations. I think parsing configurations from a given YAML file is an useful feature. Could you please explain what is the reason for making |
Mostly because serialization is hard and I wanted to focus more development effort on Here are some thoughts I shared recently about config files +
|
Hi again,
Today I was trying to override a config defined by a dataclass using a YAML file. The docs (https://brentyi.github.io/tyro/examples/03_config_systems/02_overriding_yaml/) seem to show that the use of a simple dictionary does work to override- but for a
dataclass
based config, it yields a bunch of warnings. A look into the source looks like it's looking for attributes, hence failing on a dictionary.Is this the intended behaviour? (maybe it makes sense to assume attribute based accessors considering the config itself is a dataclass- but I found this discrepancy with what's indicated in the docs a bit confusing, unless I missed something that specifies this behaviour)
For completeness here's a small example to repro. Replacing the dict with an attrdict does work.
The text was updated successfully, but these errors were encountered: