Skip to content

Validate out-of-range score_cutoff in the pure Python fallback - #520

Open
draarivpatel-ui wants to merge 1 commit into
rapidfuzz:mainfrom
draarivpatel-ui:validate-score-cutoff-python-fallback
Open

draarivpatel-ui wants to merge 1 commit into
rapidfuzz:mainfrom
draarivpatel-ui:validate-score-cutoff-python-fallback

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #509.

For out-of-range score_cutoff values the C++ implementation and the pure Python fallback behaved differently, so code that works with RAPIDFUZZ_IMPLEMENTATION=python (or on PyPy) failed with the default build:

Indel.distance("aaaa", "b", score_cutoff=-1)
# C++: OverflowError: can't convert negative value to uint64_t
# Python fallback: 0

JaroWinkler.similarity("if", "if", score_cutoff=1.2)
# C++: TypeError: score_cutoff has to be in the range of 0.0 - 1.0
# Python fallback: 0

The fallback now validates score_cutoff in the public metric functions with the same errors as the C++ implementation, mirroring the get_score_cutoff_f64 / get_score_cutoff_size_t logic in cpp_common.pxd:

  • integer metrics (distance / similarity of Hamming, Levenshtein, DamerauLevenshtein, OSA, LCSseq, Indel, Prefix, Postfix): negative cutoffs raise OverflowError
  • normalized metrics and the inherently normalized Jaro/JaroWinkler scores: values outside 0.0 - 1.0 raise TypeError

Valid inputs are unaffected; coverage is limited to the distance metrics shown in the issue.

The C++ implementation rejects out-of-range score_cutoff values while
the pure Python fallback silently accepted them, so code that runs with
RAPIDFUZZ_IMPLEMENTATION=python or on PyPy failed with the default build:

- negative cutoffs of integer metrics raise OverflowError in C++ but
  were silently accepted in the fallback
- normalized scores and Jaro/JaroWinkler metrics raise TypeError in C++
  for values outside 0.0 - 1.0 but were silently accepted in the fallback

Validate score_cutoff in the fallback with the same errors as the C++
implementation.

Fixes rapidfuzz#509
Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

prefix_len = common_prefix(s1, s2)
suffix_len = common_suffix(s1[prefix_len:], s2[prefix_len:])
return (prefix_len, suffix_len)
def validate_score_cutoff_f64(score_cutoff, worst_score, optimal_score):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is missing a newline

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Out-of-range score_cutoff: C++ raises, Python fallback returns 0

3 participants