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

struct config is not behaving as dict. when calling get #425

Closed
shay-te opened this issue Oct 28, 2020 · 4 comments · Fixed by #509
Closed

struct config is not behaving as dict. when calling get #425

shay-te opened this issue Oct 28, 2020 · 4 comments · Fixed by #509
Labels
bug Something isn't working
Milestone

Comments

@shay-te
Copy link

shay-te commented Oct 28, 2020

Describe the bug
struct config is not behaving as dict.
in python

d = {}
d['a']  # raise KeyError
d.get('a') # return null

DictConfig is expected to behave the same
currently struct DictConfg is throwing a exception

To Reproduce

cfg = OmegaConf.create({})
OmegaConf.set_struct(cfg, True)
conf.get('not exists key')

Expected behavior

conf.get('not exists key'). # will return None without raising an exception
@shay-te shay-te added the bug Something isn't working label Oct 28, 2020
@omry omry added this to the OmegaConf 2.1 milestone Oct 28, 2020
@omry
Copy link
Owner

omry commented Oct 28, 2020

Thanks for reporting, this does seem like a bug.
I will take a look at it for 2.1, for now try to specify a default None on the callsite:

cfg = OmegaConf.create({})
OmegaConf.set_struct(cfg, True)
conf.get('not exists key', None)

@guillaumegenthial
Copy link

guillaumegenthial commented Jan 3, 2021

I observed the same behavior with pop (when a default is specified).

To reproduce

from dataclasses import dataclass
from enum import Enum
from omegaconf import OmegaConf
from typing import Dict


class Color(Enum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"


@dataclass
class ColorConf:
    colors: Dict[Color, float] = None


from omegaconf import OmegaConf
from dataclasses import dataclass
from enum import Enum
from typing import Dict


class Color(Enum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"


@dataclass
class ColorConf:
    colors: Dict[Color, float] = None


config = OmegaConf.structured(ColorConf({Color.RED: 0.1}))
print(config.colors.pop(Color.RED, None))  # 0.1
print(config.colors.pop(Color.BLUE, None))  # None
config.colors.pop("blue", None)  # Errors with any key that is not Enum

which raises

omegaconf.errors.KeyValidationError: Key 'blue' is incompatible with the enum type 'Color', valid: [RED, GREEN, BLUE]
	full_key: colors.blue
	reference_type=Dict[Color, float]
	object_type=dict

Even though this is unexpected, it might be the intended behavior.

@omry
Copy link
Owner

omry commented Jan 3, 2021

@guillaumegenthial , at a glance, it looks like a different problem. can you file another issue?

@guillaumegenthial
Copy link

guillaumegenthial commented Jan 3, 2021

sure.

EDIT: #471

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.

3 participants