-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add function to check if key exists #1129
Comments
You should be able to use |
It works for main keys, but it doesn't work when going in deeper levels: from omegaconf import OmegaConf
config = OmegaConf.create({'model': {'name': 'resnet', 'num_layers': 50}})
config.model.name
# 'resnet'
"model" in config
# True
"model.name" in config
# False Is there a way to do it that I'm not aware of? |
Oh I see, indeed I don't think such a function exists right now. Note that technically |
Yes, you're right. What I'd expect is the implementation to reuse what OmegaConf.select uses internally to look for nodes. OmegaConf.select(config, "model.name")
# 'resnet'
OmegaConf.key_exists(cfg, "model.name")
# True
OmegaConf.key_exists(cfg, "model.age")
# False |
Issue Description
Feature Request: Add a built-in function to check if a key exists in OmegaConf.
Current Workaround: Currently, users need to resort to a workaround, as demonstrated below, to check if a key exists:
Suggested Enhancement
It would be beneficial to have a dedicated function or method in OmegaConf to easily check for the existence of a key within a configuration, similar to how many other configuration libraries or dictionaries support key existence checks.
Expected Behavior
Ideally, a new function or method, let's say key_exists(key), could be added to OmegaConf, allowing users to check if a key exists within the configuration. For example:
This enhancement would improve the usability and readability of OmegaConf code by eliminating the need for workarounds.
Environment
The text was updated successfully, but these errors were encountered: