Skip to content

Commit

Permalink
Re-introduced a deprecated strict flag in the Compose API (#1695)
Browse files Browse the repository at this point in the history
  • Loading branch information
omry authored Jun 24, 2021
1 parent 8640a92 commit 8fa67de
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
20 changes: 19 additions & 1 deletion hydra/compose.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import warnings
from textwrap import dedent
from typing import List, Optional

from omegaconf import DictConfig, open_dict
from omegaconf import DictConfig, OmegaConf, open_dict

from hydra.core.global_hydra import GlobalHydra
from hydra.types import RunMode
Expand All @@ -11,12 +13,14 @@ def compose(
config_name: Optional[str] = None,
overrides: List[str] = [],
return_hydra_config: bool = False,
strict: Optional[bool] = None,
) -> DictConfig:
"""
:param config_name: the name of the config
(usually the file name without the .yaml extension)
:param overrides: list of overrides for config file
:param return_hydra_config: True to return the hydra config node in the result
:param strict: DEPRECATED. If true, returned config has struct mode disabled.
:return: the composed config
"""
assert (
Expand All @@ -38,4 +42,18 @@ def compose(
if "hydra" in cfg:
with open_dict(cfg):
del cfg["hydra"]

if strict is not None:
# DEPRECATED: remove in 1.2
warnings.warn(
dedent(
"""\
The strict flag in the compose API is deprecated and will be removed in the next version of Hydra.
See https://hydra.cc/docs/upgrades/0.11_to_1.0/strict_mode_flag_deprecated for more info.
"""
)
)
OmegaConf.set_struct(cfg, strict)

return cfg
2 changes: 2 additions & 0 deletions hydra/experimental/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def compose(
config_name: Optional[str] = None,
overrides: List[str] = [],
return_hydra_config: bool = False,
strict: Optional[bool] = None,
) -> DictConfig:
from hydra import compose as real_compose

Expand All @@ -22,4 +23,5 @@ def compose(
config_name=config_name,
overrides=overrides,
return_hydra_config=return_hydra_config,
strict=strict,
)
1 change: 1 addition & 0 deletions news/1694.api_change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Re-introduced a deprecated strict flag in the Compose API
22 changes: 22 additions & 0 deletions tests/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,25 @@ def test_error_assigning_null_to_logging_config(
) -> None:
with expected:
compose(overrides=overrides)


@mark.usefixtures("initialize_hydra_no_path")
@mark.parametrize(
"strict", [param(True, id="strict=True"), param(False, id="strict=False")]
)
def test_deprecated_compose_strict_flag(strict: bool) -> None:
msg = dedent(
"""\
The strict flag in the compose API is deprecated and will be removed in the next version of Hydra.
See https://hydra.cc/docs/upgrades/0.11_to_1.0/strict_mode_flag_deprecated for more info.
"""
)

with warns(
expected_warning=UserWarning,
match=re.escape(msg),
):
cfg = compose(overrides=[], strict=strict)
assert cfg == {}
assert OmegaConf.is_struct(cfg) is strict

0 comments on commit 8fa67de

Please sign in to comment.