Fix $range and $random ignoring zero-valued optional arguments#118
Merged
zeroSteiner merged 1 commit intoJun 25, 2026
Merged
Conversation
$range(start, stop=0) silently fell through to range(start) because the 'if stop:' guard treated 0 as falsy, producing [0..start-1] instead of an empty sequence. Similarly, $range(start, stop, step=0) skipped the step validation, letting Python's range() receive a zero step without a meaningful error message. $random(boundary=0) ignored the argument and returned a float in [0, 1) instead of the expected integer 0, again because 'if boundary:' treated 0 as falsy. Fix both by using 'is not None' comparisons, which match only the absence of an argument rather than its falsy value. Add regression tests for $range(5, 0) and $random(0).
Owner
|
Yup, that looks legit. Thanks for reporting and submitting a fix for this. |
Owner
|
LGTM |
Owner
|
Patch shipped in 5.0.1. Thanks again. |
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.
What
_builtin_rangeand_builtin_randomuse truthiness checks (if stop:,if step:,if boundary:) to detect whether optional numeric argumentswere supplied. This is wrong when the argument value is
0, which isfalsy but a valid integer.
$range(start, stop=0)stop=0evaluated as falsy, so the function ignored it and calledrange(int(start))=range(5)instead ofrange(5, 0)=[].$range(start, stop, step=0)step=0evaluated as falsy, so the zero-step check was skipped.Python's
range()then raised a bareValueErrorrather than alibrary
FunctionCallError.$random(boundary=0)boundary=0evaluated as falsy, so the function ignored it and returnedrandom.random()instead ofrandom.randint(0, 0)=0.Fix
Replace
if stop:/if step:/if boundary:withis not Nonecomparisons in
lib/rule_engine/builtins.py. Add regression tests for$range(5, 0)and$random(0).This pull request was prepared with the assistance of AI, under my direction and review.