Fix range patterns and Integer comparisons against Response::Status - #847
Open
lineoffligbot wants to merge 1 commit into
Open
Fix range patterns and Integer comparisons against Response::Status#847lineoffligbot wants to merge 1 commit into
lineoffligbot wants to merge 1 commit into
Conversation
Integer#<=> requires the numeric coercion protocol (#coerce) to compare with custom numeric types. Status lacked #coerce, so `500 <=> status` returned nil and prevented Range#=== from matching, causing pattern matching like `case response in status: 500..599` to fail silently. This adds #coerce returning [other, code]. Co-authored-by: Hakan Ensari <hakanensari@gmail.com>
hakanensari
force-pushed
the
fix/status-integer-comparison
branch
from
August 16, 2026 14:14
0d67de4 to
4488a49
Compare
Contributor
Author
|
A note on the failing steep job: it is environment drift, not this patch. The Gemfile does not lock rbs ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Pattern matching a response status against a range silently never matches:
Equality patterns (
in status: 429) work, which makes the range failure easy to miss: the pattern doesn't error, it just falls through. This is the exact usage recommended in the discussion that closed #839 (in status: 500..599 then retry), and it would apply equally to theStatusErrordeconstruction proposed there, since that delegates to the sameResponse#deconstruct_keys.Root cause
deconstruct_keysexposesstatus:as aResponse::Status. A range pattern evaluates(500..599) === status, which calls500 <=> status.Integer#<=>resolves unfamiliar operands through the numeric coercion protocol (instance#coerce), not through#to_int.Statusdefines#<=>,#to_i, and#to_int, but no instance#coerce(the class-levelStatus.coerceis an unrelated factory), so500 <=> statusreturnsniland the range never matches.Fix
Implement the protocol:
Now
500 <=> statusreturns-1and(500..599) === statusreturnstrue, incase/in,case/when, and mixed sorting alike. I chose this over makingdeconstruct_keysemitstatus.to_i, which would change the deconstruction shape and breakin status: {code: ...}matching.Notes
coerce, so1 + statusnow returns504instead of raisingTypeError(status + 1still raises, sinceStatusdefines no+). This seems consistent with the class already delegatingto_i,to_int, andhashto the code and includingComparable.Stringdoesn't use the numeric protocol, andin status: "400"still (correctly) never matches.Tests pin all three layers:
500 <=> status,Range#===, and the user-facingin status: 500..599onResponse, plus thecoercepair itself. RBS signature included. Mutant kills 100% of mutations on the diff.