Skip to content

Commit

Permalink
#124: Fix eq fails if object contains unresolveable values
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Jan 9, 2020
1 parent f566118 commit ea514de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions news/124.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix eq fails if object contains unresolveable values
9 changes: 7 additions & 2 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ def get_value_kind(value: Any, return_match_list: bool = False) -> Any:
:param return_match_list: True to return the match list as well
:return: ValueKind
"""
if not isinstance(value, str):
return ValueKind.VALUE
from .nodes import ValueNode

if isinstance(value, ValueNode):
value = value.value()

key_prefix = r"\${(\w+:)?"
legal_characters = r"([\w\.%_ \\,-]*?)}"
Expand All @@ -217,6 +219,9 @@ def ret(
if value == "???":
return ret(ValueKind.MANDATORY_MISSING)

if not isinstance(value, str):
return ValueKind.VALUE

match_list = list(re.finditer(key_prefix + legal_characters, value))
if len(match_list) == 0:
return ret(ValueKind.VALUE)
Expand Down
20 changes: 16 additions & 4 deletions omegaconf/basecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,26 @@ def _item_eq(

v1 = c1.get_node(k1)
v2 = c2.get_node(k2)
v1_kind = get_value_kind(v1)
v2_kind = get_value_kind(v2)

if isinstance(v1, ValueNode):
v1 = v1.value()
if isinstance(v1, str):
v1 = c1._resolve_single(v1)
if isinstance(v2, ValueNode):
v2 = v2.value()
if isinstance(v2, str):
v2 = c2._resolve_single(v2)

# special case for two interpolations. just compare them literally.
# This is helping in cases where the two interpolations are not resolvable
# but the objects are still considered equal.
if v1_kind in (
ValueKind.STR_INTERPOLATION,
ValueKind.INTERPOLATION,
) and v2_kind in (ValueKind.STR_INTERPOLATION, ValueKind.INTERPOLATION):
return True
if isinstance(v1, str):
v1 = c1._resolve_single(v1)
if isinstance(v2, str):
v2 = c2._resolve_single(v2)

if isinstance(v1, BaseContainer) and isinstance(v2, BaseContainer):
if not BaseContainer._config_eq(v1, v2):
Expand Down
3 changes: 3 additions & 0 deletions tests/structured_conf/test_config_eq.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
# With interpolations
([10, "${0}"], [10, 10]),
(dict(a=12, b="${a}"), dict(a=12, b=12)),
# With missing interpolation
([10, "${0}"], [10, 10]),
(dict(a="${missing}"), dict(a="${missing}")),
],
)
def test_list_eq(l1: List[Any], l2: List[Any]) -> None:
Expand Down

0 comments on commit ea514de

Please sign in to comment.