Skip to content
Open
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ Each release links to full notes on the

### Fixed

- `FuzzyComparator` raises `ValueError` for an unknown `method` instead of

@vawsgit vawsgit Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: an unknown method used to load and score as ratio, and now it fails at construction, on the Python path and in configs. On this head, from_json_schema() raises Invalid x-aws-stickler-comparator 'FuzzyComparator' in field 'n': Unknown fuzzy method 'token_sort', and model_from_json() raises too. The closest [Unreleased] entry is #607 (model_from_json() now raises for match_threshold on a primitive field, where 1.0.x warned), and it sits under ### Changed, not ### Fixed. Moving this entry there, or marking it **Breaking:** as #646 does, would make it show in the release notes as a change users must act on.

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`,
`token_sort_ratio` or `token_set_ratio`.

- **A comparator key on a nested model now warns instead of being dropped in
silence or refused.** A nested model is scored field by field, so its
comparator is never called, as on the Python path. `comparator` on a
Expand Down
11 changes: 9 additions & 2 deletions src/stickler/comparators/fuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(

Raises:
ImportError: If rapidfuzz library is not available
ValueError: If method is not one of the options above
"""
super().__init__(threshold=threshold)

Expand All @@ -64,12 +65,18 @@ def __init__(
self._normalize = normalize

# Select the appropriate fuzzy matching function
self._fuzzy_func = {
fuzzy_funcs = {
"ratio": fuzz.ratio,
"partial_ratio": fuzz.partial_ratio,
"token_sort_ratio": fuzz.token_sort_ratio,
"token_set_ratio": fuzz.token_set_ratio,
}.get(method, fuzz.ratio)
}
if method not in fuzzy_funcs:
raise ValueError(
f"Unknown fuzzy method {method!r}. "
f"Expected one of: {', '.join(fuzzy_funcs)}"
)
self._fuzzy_func = fuzzy_funcs[method]

@property
def name(self) -> str:
Expand Down
7 changes: 7 additions & 0 deletions tests/common/comparators/test_comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
to ensure they work correctly and maintain compatibility with existing code.
"""

import pytest

from stickler.comparators import (
LevenshteinComparator,
Expand Down Expand Up @@ -174,6 +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", ""])
def test_unknown_method_raises(self, method):
"""An unknown method is rejected instead of falling back to ratio."""
with pytest.raises(ValueError, match="Unknown fuzzy method"):
FuzzyComparator(method=method)

def test_binary_compare(self):
"""Test binary_compare returns correct (tp, fp) tuples."""
# Exact match should return (1, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setup_method(self):
self.fuzzy_token_sort = FuzzyComparator(method="token_sort_ratio")
self.fuzzy_token_set = FuzzyComparator(method="token_set_ratio")
self.fuzzy_partial = FuzzyComparator(method="partial_ratio")
self.fuzzy_weighted = FuzzyComparator(method="weighted_ratio")
self.fuzzy_weighted = FuzzyComparator(method="ratio")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now builds the same comparator as self.fuzzy_ratio on line 24, but the tests still treat it as its own method: test_mix_and_match collects it as weighted_score (152 and 163), and 127 and 180 assert on it. Could you drop this line, use self.fuzzy_ratio at 127 and 180, and remove weighted_score at 152 and 163? All 7 tests in the file still pass with that change.


def test_names_and_people(self):
"""Test comparators with names and people data."""
Expand Down