1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import re
from typing import Any
from pytest import mark, param, raises, warns
from omegaconf import OmegaConf
from omegaconf.errors import InterpolationResolutionError
@mark.parametrize(
("cfg", "key", "expected_value", "expected_warning"),
[
param(
{"a": 10, "b": "${oc.deprecated: a}"},
"b",
10,
"'b' is deprecated. Change your code and config to use 'a'",
id="value",
),
param(
{"a": 10, "b": "${oc.deprecated: a, '$OLD_KEY is deprecated'}"},
"b",
10,
"b is deprecated",
id="value-custom-message",
),
param(
{
"a": 10,
"b": "${oc.deprecated: a, ${warning}}",
"warning": "$OLD_KEY is bad, $NEW_KEY is good",
},
"b",
10,
"b is bad, a is good",
id="value-custom-message-config-variable",
),
param(
{"a": {"b": 10}, "b": "${oc.deprecated: a}"},
"b",
OmegaConf.create({"b": 10}),
"'b' is deprecated. Change your code and config to use 'a'",
id="dict",
),
param(
{"a": {"b": 10}, "b": "${oc.deprecated: a}"},
"b.b",
10,
"'b' is deprecated. Change your code and config to use 'a'",
id="dict_value",
),
param(
{"a": [0, 1], "b": "${oc.deprecated: a}"},
"b",
OmegaConf.create([0, 1]),
"'b' is deprecated. Change your code and config to use 'a'",
id="list",
),
param(
{"a": [0, 1], "b": "${oc.deprecated: a}"},
"b[1]",
1,
"'b' is deprecated. Change your code and config to use 'a'",
id="list_value",
),
],
)
def test_deprecated(
cfg: Any, key: str, expected_value: Any, expected_warning: str
) -> None:
cfg = OmegaConf.create(cfg)
with warns(UserWarning, match=re.escape(expected_warning)):
value = OmegaConf.select(cfg, key)
assert value == expected_value
assert type(value) == type(expected_value)
@mark.parametrize(
("cfg", "error"),
[
param(
{"a": "${oc.deprecated: z}"},
"ConfigKeyError raised while resolving interpolation:"
" In oc.deprecated resolver at 'a': Key not found: 'z'",
id="target_not_found",
),
param(
{"a": "${oc.deprecated: 111111}"},
"TypeError raised while resolving interpolation: oc.deprecated:"
" interpolation key type is not a string (int)",
id="invalid_key_type",
),
param(
{"a": "${oc.deprecated: b, 1000}", "b": 10},
"TypeError raised while resolving interpolation: oc.deprecated:"
" interpolation message type is not a string (int)",
id="invalid_message_type",
),
],
)
def test_deprecated_target_not_found(cfg: Any, error: str) -> None:
cfg = OmegaConf.create(cfg)
with raises(
InterpolationResolutionError,
match=re.escape(error),
):
cfg.a
|