Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# (C) 2026 GoodData Corporation
"""Recursive subset matcher for alert filter comparison."""

from decimal import Decimal, InvalidOperation
from typing import Any


Expand All @@ -12,7 +13,9 @@ def deep_subset(expected: Any, actual: Any) -> bool:
claims the first unused actual element it deep-subset-matches. Sufficient
for alert filters (small, distinct-type lists); may miss valid matchings
when two expected items could match the same actual item.
- other: equality.
- other: equality. A number also matches a numeric string of the same value,
because the agent's tools accept both (`500` and `"500"` set the same threshold);
two strings, and booleans, still compare exactly.
"""
if isinstance(expected, dict):
if not isinstance(actual, dict):
Expand All @@ -32,4 +35,14 @@ def deep_subset(expected: Any, actual: Any) -> bool:
if not matched:
return False
return True
return expected == actual
return expected == actual or _same_number(expected, actual) or _same_number(actual, expected)


def _same_number(input_a: Any, input_b: Any) -> bool:
if not isinstance(input_a, (int, float)) or isinstance(input_a, bool) or not isinstance(input_b, str):
return False
# Decimal, not float: float("9007199254740993") rounds to 9007199254740992.
try:
return Decimal(input_b) == Decimal(str(input_a))
except InvalidOperation:
return False
21 changes: 21 additions & 0 deletions packages/gooddata-eval/tests/test_deep_subset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# (C) 2026 GoodData Corporation
import pytest
from gooddata_eval.core.evaluators._deep_subset import deep_subset


Expand Down Expand Up @@ -32,3 +33,23 @@ def test_deep_subset_nested():
expected = {"filter": {"in": {"values": ["abc"]}}}
actual = {"filter": {"in": {"values": ["abc"]}, "extra": True}}
assert deep_subset(expected, actual) is True


@pytest.mark.parametrize(
("expected", "actual", "matches"),
[
pytest.param(500, "500", True, id="int-and-numeric-string"),
pytest.param(0.15, "0.15", True, id="float-and-numeric-string"),
pytest.param(500, "500.0", True, id="int-and-float-string"),
pytest.param("500", 500, True, id="numeric-string-and-int"),
pytest.param(9007199254740993, "9007199254740993", True, id="large-int-and-its-string"),
pytest.param({"threshold": 10000}, {"threshold": "10000", "operator": "LESS_THAN"}, True, id="tool-args"),
pytest.param(500, "501", False, id="different-value"),
pytest.param(9007199254740992, "9007199254740993", False, id="large-int-off-by-one"),
pytest.param(500, "five hundred", False, id="non-numeric-string"),
pytest.param("500", "500.0", False, id="two-strings-stay-exact"),
pytest.param(True, "1", False, id="bool-is-not-a-number"),
],
)
def test_deep_subset_number_matches_numeric_string(expected, actual, matches):
assert deep_subset(expected, actual) is matches
Loading