Python: fix {{range}} handlebars helper crashing on consecutive invalid args#14120
Open
Osamaali313 wants to merge 1 commit into
Open
Python: fix {{range}} handlebars helper crashing on consecutive invalid args#14120Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
_range (backing the {{range}} helper) dropped non-integer args with args.pop(index) while iterating the same list via enumerate(args). Removing an element shifts the remaining items left, so the arg right after a dropped one is skipped and never validated; that leftover string then reaches range(), raising TypeError. A single invalid arg works (existing test: range 'a' 5 -> [0,1,2,3,4]), but two consecutive invalid args (range 'a' 'b' 5) crash, contradicting the helper's drop-invalid-args behavior.
Build the converted-args list without mutating during iteration, and also tolerate int(None) (TypeError) in addition to ValueError. All existing outputs are unchanged. Adds a regression test.
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
_range(inpython/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py, backing the{{range}}helper) drops non-integer args by mutating the list while iterating it:args.pop(index)shifts the remaining elements left, so the arg immediately after a dropped one is skipped and never validated/converted. That leftover string then reachesrange(), raisingTypeError.A single invalid arg works (existing test:
{{range 'a' 5}}→[0, 1, 2, 3, 4]), but two consecutive invalid args crash:which contradicts the helper's drop-invalid-args behavior.
Reproduction (real function)
Fix
Build the converted list without mutating during iteration (and tolerate
int(None)→TypeErroralongsideValueError):All existing outputs are unchanged.
Test
Adds
("range", "'a' 'b' 5", "[0, 1, 2, 3, 4]")to the parametrized helper test inpython/tests/unit/prompt_template/test_handlebars_prompt_template.py— it raisesTypeErroron the old code and passes after the fix.