diff --git a/hydra/_internal/config_repository.py b/hydra/_internal/config_repository.py index eb0862b28f..c9a4f6667c 100644 --- a/hydra/_internal/config_repository.py +++ b/hydra/_internal/config_repository.py @@ -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 @@ -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: @@ -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) diff --git a/news/1952.maintenance b/news/1952.maintenance new file mode 100644 index 0000000000..d0fd668ffe --- /dev/null +++ b/news/1952.maintenance @@ -0,0 +1 @@ +For version_base >= 1.2, remove deprecated "old optional" defaults list syntax diff --git a/tests/defaults_list/test_defaults_list.py b/tests/defaults_list/test_defaults_list.py index 91374a5950..b13dce5a99 100644 --- a/tests/defaults_list/test_defaults_list.py +++ b/tests/defaults_list/test_defaults_list.py @@ -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, @@ -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(