Skip to content

Commit

Permalink
refactor DictConfig._validate_non_optional, standardize err messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Oct 6, 2021
1 parent 7fb6fe5 commit d4fca02
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
34 changes: 14 additions & 20 deletions omegaconf/dictconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,33 +236,27 @@ def _validate_merge(self, value: Any) -> None:
)
raise ValidationError(msg)

def _validate_non_optional(self, key: Any, value: Any) -> None:
def _validate_non_optional(self, key: Optional[DictKeyType], value: Any) -> None:
if _is_none(value, resolve=True, throw_on_resolution_failure=False):

if key is not None:
child = self._get_node(key)
if child is not None:
assert isinstance(child, Node)
if not child._is_optional():
self._format_and_raise(
key=key,
value=value,
cause=ValidationError("child '$FULL_KEY' is not Optional"),
)
field_is_optional = child._is_optional()
else:
is_optional, _ = _resolve_optional(self._metadata.element_type)
if not is_optional:
self._format_and_raise(
key=key,
value=value,
cause=ValidationError("field '$FULL_KEY' is not Optional"),
)
else:
if not self._is_optional():
self._format_and_raise(
key=None,
value=value,
cause=ValidationError("field '$FULL_KEY' is not Optional"),
field_is_optional, _ = _resolve_optional(
self._metadata.element_type
)
else:
field_is_optional = self._is_optional()

if not field_is_optional:
self._format_and_raise(
key=key,
value=value,
cause=ValidationError("field '$FULL_KEY' is not Optional"),
)

def _raise_invalid_value(
self, value: Any, value_type: Any, target_type: Any
Expand Down
2 changes: 1 addition & 1 deletion tests/structured_conf/test_structured_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_merge_error_override_bad_type(self, module: Any) -> None:

def test_error_message(self, module: Any) -> None:
cfg = OmegaConf.structured(module.StructuredOptional)
msg = re.escape("child 'not_optional' is not Optional")
msg = re.escape("field 'not_optional' is not Optional")
with raises(ValidationError, match=msg):
cfg.not_optional = None

Expand Down
4 changes: 2 additions & 2 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def finalize(self, cfg: Any) -> None:
create=lambda: OmegaConf.structured(StructuredWithMissing),
op=lambda cfg: OmegaConf.update(cfg, "num", None),
exception_type=ValidationError,
msg="child 'num' is not Optional",
msg="field 'num' is not Optional",
parent_node=lambda cfg: cfg,
child_node=lambda cfg: cfg._get_node("num"),
object_type=StructuredWithMissing,
Expand Down Expand Up @@ -370,7 +370,7 @@ def finalize(self, cfg: Any) -> None:
),
op=lambda cfg: setattr(cfg, "foo", None),
exception_type=ValidationError,
msg="child 'foo' is not Optional",
msg="field 'foo' is not Optional",
key="foo",
full_key="foo",
child_node=lambda cfg: cfg.foo,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_none_assignment_and_merging_in_dict(
data = {"node": node}
cfg = OmegaConf.create(obj=data)
verify(cfg, "node", none=False, opt=False, missing=False, inter=False)
msg = "child 'node' is not Optional"
msg = "field 'node' is not Optional"
with raises(ValidationError, match=re.escape(msg)):
cfg.node = None

Expand Down
2 changes: 1 addition & 1 deletion tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def test_merge(
match=re.escape(
dedent(
"""\
child 'foo' is not Optional
field 'foo' is not Optional
full_key: foo
object_type=dict"""
)
Expand Down

0 comments on commit d4fca02

Please sign in to comment.