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

deprecating auto expansion of nested dataclasses without default value #413

Closed
wants to merge 1 commit into from
Closed
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/412.api_change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Automatic expansion of nested dataclasses without a default value is deprecated
19 changes: 19 additions & 0 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import re
import string
import sys
import warnings
from enum import Enum
from textwrap import dedent
from typing import Any, Dict, List, Match, Optional, Tuple, Type, Union

import yaml
Expand Down Expand Up @@ -193,6 +195,14 @@ def get_attr_data(obj: Any) -> Dict[str, Any]:
value = attrib.default
if value == attr.NOTHING:
if is_nested:
msg = dedent(
f"""
The field `{name}` of type '{type_str(type_)}' does not have a default value.
The behavior of OmegaConf for such cases is changing in OmegaConf 2.1.
See https://github.com/omry/omegaconf/issues/412 for more details.
"""
)
warnings.warn(category=UserWarning, message=msg, stacklevel=8)
value = type_
else:
_raise_missing_error(obj, name)
Expand Down Expand Up @@ -227,6 +237,15 @@ def get_dataclass_data(obj: Any) -> Dict[str, Any]:
else:
if field.default_factory == dataclasses.MISSING: # type: ignore
if is_nested:
msg = dedent(
f"""
The field `{name}` of type '{type_str(type_)}' does not have a default value.
The behavior of OmegaConf for such cases is changing in OmegaConf 2.1.
See https://github.com/omry/omegaconf/issues/412 for more details.
"""
)
warnings.warn(category=UserWarning, message=msg, stacklevel=8)

value = type_
else:
_raise_missing_error(obj, name)
Expand Down
6 changes: 4 additions & 2 deletions tests/structured_conf/test_structured_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def validate(cfg: DictConfig) -> None:
cfg.default_value = module.Nested()
assert cfg.default_value == module.Nested()

conf1 = OmegaConf.structured(module.NestedConfig)
with pytest.warns(UserWarning):
conf1 = OmegaConf.structured(module.NestedConfig)
validate(conf1)
conf1 = OmegaConf.structured(module.NestedConfig(default_value=module.Nested()))
validate(conf1)
Expand Down Expand Up @@ -183,7 +184,8 @@ def validate(cfg: DictConfig) -> None:

def test_assignment_to_nested_structured_config(self, class_type: str) -> None:
module: Any = import_module(class_type)
conf = OmegaConf.structured(module.NestedConfig)
with pytest.warns(UserWarning):
conf = OmegaConf.structured(module.NestedConfig)
with pytest.raises(ValidationError):
conf.default_value = 10

Expand Down