Skip to content

Commit

Permalink
remove handling of deprecated "old optional" defaults list syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Apr 16, 2022
1 parent d8fb813 commit a78d1f6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
33 changes: 18 additions & 15 deletions hydra/_internal/config_repository.py
Expand Up @@ -15,6 +15,7 @@
read_write,
)

from hydra import version
from hydra.core.config_search_path import ConfigSearchPath
from hydra.core.object_type import ObjectType
from hydra.plugins.config_source import ConfigResult, ConfigSource
Expand Down Expand Up @@ -190,10 +191,11 @@ def issue_deprecated_name_warning() -> None:
for item in defaults._iter_ex(resolve=False):
default: InputDefault
if isinstance(item, DictConfig):
old_optional = None
if len(item) > 1:
if "optional" in item:
old_optional = item.pop("optional")
if not version.base_at_least("1.2"):
old_optional = None
if len(item) > 1:
if "optional" in item:
old_optional = item.pop("optional")
keys = list(item.keys())

if len(keys) > 1:
Expand All @@ -209,23 +211,24 @@ def issue_deprecated_name_warning() -> None:
keywords = ConfigRepository.Keywords()
self._extract_keywords_from_config_group(config_group, keywords)

if not keywords.optional and old_optional is not None:
keywords.optional = old_optional
if not version.base_at_least("1.2"):
if not keywords.optional and old_optional is not None:
keywords.optional = old_optional

node = item._get_node(key)
assert node is not None and isinstance(node, Node)
config_value = node._value()

if old_optional is not None:
# DEPRECATED: remove in 1.2
msg = dedent(
f"""
In {config_path}: 'optional: true' is deprecated.
Use 'optional {key}: {config_value}' instead.
Support for the old style will be removed in Hydra 1.2"""
)
if not version.base_at_least("1.2"):
if old_optional is not None:
msg = dedent(
f"""
In {config_path}: 'optional: true' is deprecated.
Use 'optional {key}: {config_value}' instead.
Support for the old style is removed for Hydra version_base >= 1.2"""
)

deprecation_warning(msg)
deprecation_warning(msg)

if config_value is not None and not isinstance(
config_value, (str, list)
Expand Down
1 change: 1 addition & 0 deletions news/1952.maintenance
@@ -0,0 +1 @@
For version_base >= 1.2, remove deprecated "old optional" defaults list syntax
59 changes: 42 additions & 17 deletions tests/defaults_list/test_defaults_list.py
Expand Up @@ -5,6 +5,7 @@

from pytest import mark, param, raises, warns

from hydra import version
from hydra._internal.defaults_list import create_defaults_list
from hydra.core.default_element import (
ConfigDefault,
Expand Down Expand Up @@ -80,23 +81,47 @@ def test_loaded_defaults_list(
),
],
)
def test_deprecated_optional(
config_path: str, expected_list: List[InputDefault]
) -> None:
repo = create_repo()
warning = dedent(
"""
In optional_deprecated: 'optional: true' is deprecated.
Use 'optional group1: file1' instead.
Support for the old style will be removed in Hydra 1.2"""
)
with warns(
UserWarning,
match=re.escape(warning),
):
result = repo.load_config(config_path=config_path)
assert result is not None
assert result.defaults_list == expected_list
class TestDeprecatedOptional:
def test_version_base_1_1(
self,
config_path: str,
expected_list: List[InputDefault],
) -> None:
curr_base = version.getbase()
version.setbase("1.1")
repo = create_repo()
warning = dedent(
"""
In optional_deprecated: 'optional: true' is deprecated.
Use 'optional group1: file1' instead.
Support for the old style is removed for Hydra version_base >= 1.2"""
)
with warns(
UserWarning,
match=re.escape(warning),
):
result = repo.load_config(config_path=config_path)
assert result is not None
assert result.defaults_list == expected_list
version.setbase(str(curr_base))

@mark.parametrize("version_base", ["1.2", None])
def test_version_base_1_2(
self,
config_path: str,
expected_list: List[InputDefault],
version_base: Optional[str],
) -> None:
curr_base = version.getbase()
version.setbase(version_base)
repo = create_repo()
err = "In optional_deprecated: Too many keys in default item {'group1': 'file1', 'optional': True}"
with raises(
ValueError,
match=re.escape(err),
):
repo.load_config(config_path=config_path)
version.setbase(str(curr_base))


def _test_defaults_list_impl(
Expand Down

0 comments on commit a78d1f6

Please sign in to comment.