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

DictConfig silently ignores setdefault #304

Closed
an1lam opened this issue Jul 15, 2020 · 7 comments · Fixed by #347
Closed

DictConfig silently ignores setdefault #304

an1lam opened this issue Jul 15, 2020 · 7 comments · Fixed by #347
Assignees
Labels
bug Something isn't working

Comments

@an1lam
Copy link

an1lam commented Jul 15, 2020

If I run the following code using omegaconf version 2.0.0 (Python 3.7.6, anaconda version):

import omegaconf

if __name__ == "__main__":
    cfg = omegaconf.DictConfig({"a": "b"})
    print(cfg)

    cfg.setdefault("c", [])
    print(cfg)

    cfg["c"] = []
    print(cfg)

I'll get the following output:

2.0.0
{'a': 'b'}
{'a': 'b'}
{'a': 'b', 'c': []}

Observe that 'c' does not get set by setdefault even though DictConfig inherits from MutableMapping, which in its spec, states that it should support setdefault.

I feel like this should in order of preference:

  • work;
  • throw an exception rather than silently fail;
  • be documented in the DictConfig API.

Since I'm assuming none of these are that hard to implement, I could potentially implement the preferred solution myself. Just would want some guidance on which you think makes the most sense and, if it's the first option, what's the right way to do it.

@omry
Copy link
Owner

omry commented Jul 15, 2020

Hi, it's really curious that this went undetected because OmegaConf is passing a strict mypy check.

Since the intention is for OmegaConf DictConfig to be as compatible as possible with dict (with the exception of the base class) - I would like this to work.
go ahead and send a PR adding it, be sure to add tests covering it and a news fragment (this is more or less correct for OmegaConf too).

@an1lam
Copy link
Author

an1lam commented Jul 15, 2020

So it's because the method exists on the underlying base class, but it relies on self[key] throwing KeyError when it fails. One way to fix this would be to override the method, another would be to throw KeyError when bracket accesses fail. Which do you prefer?

@omry
Copy link
Owner

omry commented Jul 15, 2020

[] throws a key error when struct mode is enabled. this is by design. changing this behavior for regular mode is a significant breaking change.

Let's have our own implementation
be sure to test it with and without struct mode.

@omry
Copy link
Owner

omry commented Jul 15, 2020

If you are using OmegaConf through Hydra, struct mode is becoming the default in 1.0.
If you are using it directly, you can just set the flag after initialization.

One more thing:
Use OmegaConf.create() to create the DictConfig object, users are not expected to create it directly and I make no promises with respect to breaking API changes for it's constructor.

@omry
Copy link
Owner

omry commented Jul 15, 2020

Since you are actively working on it, setting the milestone to 2.0.1.

@omry omry added this to the OmegaConf 2.0.1 milestone Jul 15, 2020
@omry omry added the bug Something isn't working label Jul 15, 2020
@an1lam
Copy link
Author

an1lam commented Jul 15, 2020

👍 just to set expectations, I probably won't get to it until the weekend.

@omry
Copy link
Owner

omry commented Jul 15, 2020

That's fine.

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

Successfully merging a pull request may close this issue.

2 participants