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

api change: dictconfig.get("missing") returns None #528

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/527.api_change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DictConfig get access, e.g. `cfg.get("foo")`, now returns `None` if the key "foo" does not exist
8 changes: 4 additions & 4 deletions omegaconf/dictconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ def __delitem__(self, key: DictKeyType) -> None:

del self.__dict__["_content"][key]

def get(self, key: DictKeyType, default_value: Any = DEFAULT_VALUE_MARKER) -> Any:
def get(self, key: DictKeyType, default_value: Any = None) -> Any:
"""Return the value for `key` if `key` is in the dictionary, else
`default_value` (defaulting to `None`)."""
try:
return self._get_impl(key=key, default_value=default_value)
except ConfigAttributeError:
return None
except Exception as e:
except KeyValidationError as e:
self._format_and_raise(key=key, value=None, cause=e)

def _get_impl(self, key: DictKeyType, default_value: Any) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_is_missing(
) -> None:
cfg = OmegaConf.create(cfg)
with expectation:
cfg.get(key)
cfg[key]

assert OmegaConf.is_missing(cfg, key) == expected_is_missing
OmegaConf.set_struct(cfg, True)
Expand Down