Skip to content
Open
Prev Previous commit
Next Next commit
fix: reject non-string FuzzyComparator methods
  • Loading branch information
pcbeingused33 committed Nov 30, 2026
commit a9f8599f28a4e28905517d2cd3ddbe5ceed7459f
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ Each release links to full notes on the
importer already refused the same misplacement
([#607](https://github.com/awslabs/stickler/issues/607)).

- `FuzzyComparator` raises `ValueError` for an unknown `method` instead of
silently using `ratio`. A misspelled name such as `"token_sort"`, in Python or
in a config, must be changed to one of `ratio`, `partial_ratio`,
- `FuzzyComparator` raises `ValueError` for an unknown or non-string `method`.
Unknown strings previously fell back to `ratio`. Python and config values
must use one of `ratio`, `partial_ratio`,
`token_sort_ratio` or `token_set_ratio`.

### Removed
Expand Down
2 changes: 1 addition & 1 deletion src/stickler/comparators/fuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
"token_sort_ratio": fuzz.token_sort_ratio,
"token_set_ratio": fuzz.token_set_ratio,
}
if method not in fuzzy_funcs:
if not isinstance(method, str) or method not in fuzzy_funcs:
raise ValueError(
f"Unknown fuzzy method {method!r}. "
f"Expected one of: {', '.join(fuzzy_funcs)}"
Expand Down
14 changes: 8 additions & 6 deletions tests/common/comparators/test_comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ def test_token_methods(self):
# Test token set (common tokens matter)
assert token_set.compare("python is great and fast", "python is fast") > 0.8

@pytest.mark.parametrize("method", ["token_sort", "weighted_ratio", ""])
@pytest.mark.parametrize(
"method",
["token_sort", "weighted_ratio", "", ["ratio"], {"method": "ratio"}],
)
def test_unknown_method_raises(self, method):
"""An unknown method is rejected instead of falling back to ratio."""
"""Reject unknown names and non-string method values with ValueError."""
with pytest.raises(ValueError, match="Unknown fuzzy method"):
FuzzyComparator(method=method)

Expand All @@ -195,7 +198,6 @@ def test_binary_compare(self):

# Test token methods with binary comparison
token_set = FuzzyComparator(method="token_set_ratio", threshold=0.7)
assert (
token_set.binary_compare("python is great and fast", "python is fast")
== (1, 0)
)
assert token_set.binary_compare(
"python is great and fast", "python is fast"
) == (1, 0)